This set of SQL interview questions uses a bookstore database with the following tables: books, authors, transactions, and customers.
books
book_idINT (KEY)titleVARCHARauthor_idINTpublication_dateDATEcategoryVARCHARpriceDOUBLE
authors
author_idINT (KEY)first_nameVARCHARlast_nameVARCHARbirthdayDATEwebsite_urlVARCHAR
transactions
transaction_idINT (KEY)book_idINTcustomer_idINTpayment_amountDOUBLEbook_countINTtax_rateDOUBLEdiscount_rateDOUBLEtransaction_dateDATEpayment_typeVARCHAR
customers
customer_idINT (KEY)first_nameVARCHARlast_nameVARCHARregistration_dateDATEinterested_in_categoriesVARCHARis_rewards_memberBOOLEANinvited_by_customer_idINT
Questions
- What was the total value of sales and the number of unique paying customers, grouped and sorted in descending order by payment type?
- Existing customers can invite other people to sign up to the bookstore. Find the IDs of the top 5 customers, ordered by the average payment per book made by the people they invited.
- Find the total number of authors. What percentage of them have a website URL that contains
.com, and what percentage never made a sale?
This Meta SQL problem focuses on grouped aggregation, relationship traversal, and percentage-based analysis. The first query groups transactions by payment type and computes total sales and the number of distinct paying customers. The second query follows the invitation chain through customers.invited_by_customer_id, then ranks inviters by the average payment per book made by the customers they invited. The third query is an author-level analysis that counts all authors, measures the share whose website URL contains ".com", and finds the share of authors who never made a sale. Overall, the key techniques are GROUP BY, COUNT(DISTINCT …), joins, and conditional aggregation.