How can I create an SQL query for finding the percentage of elements in a database using keywords?

I am currently working for an online bookstore that wants to analyze the popularity of book genres among their clients or customers. For this, I have a table of databases whose name is “Books” I which have columns like “BookID”, “Title”, ”Genre”, and “keyword”. How can I write an SQL query to calculate the percentage of books in each genre or category that contains a specific keyword? Here is the table structure given of the table “Books”

CREATE TABLE Books (
    BookID INT,
    Title VARCHAR(100),
    Genre VARCHAR(50),
    Keywords VARCHAR(200)
);

I want to write an SQL query for finding the percentage of books in each category or genre that contains keywords like “technology.”

Level up your career with Online SQL Server course! Start your journey to success today. Enroll now and unleash your full potential!

Answered by Charles Parr

In the context of SQL, you can find the percentage in SQL for the genre of the books by just providing the keyword (technology in this case). Here is the method given to showcase the process:-

SELECT Genre, 
    (COUNT(CASE WHEN Keywords LIKE ‘%technology%’ THEN 1 END) / COUNT(*)) * 100 AS Percentage
FROM Books
GROUP BY Genre;

This above query would provide the result you wanted. This would show each genre and the related percentage of books within that genre which would include the term or keyword “technology”.



Your Answer

Interviews

Parent Categories