solulubeta
Sign in
Module 05

SQL

Seventy questions on one realistic database, run on a real PostgreSQL engine inside your browser.

Every question uses the same Campus Store database (products, customers, sales), so you spend your time on SQL, not on learning a new schema each time. Run checks your query on the sample data; Submit also runs it on a hidden dataset with different values, so hard-coded answers don't pass.

  • Write SELECT, WHERE, ORDER BY, GROUP BY and HAVING without looking anything up
  • Join tables correctly, including left joins and "find what's missing" questions
  • Use subqueries, CASE, window functions and CTEs — the usual interview filter
  • Change data safely with INSERT, UPDATE, constraints, views and transactions
77
problems
10
free to start
~4h
of study
01

Reading a table

SELECT, WHERE, AND / OR, BETWEEN and ORDER BY.

02

Summarising data

COUNT, SUM, AVG, GROUP BY, DISTINCT, LIMIT and dates.

03

Joining tables

Inner and left joins, LIKE and finding what's missing.

03.01 · Lesson

Joining tables

Combining sales with products and customers: inner joins, left joins, LIKE and finding what's missing.

3 min Free
03.02 · SQL

Name the product on every sale

The sales table only stores product_id. Return sale_id and product_name for every sale.

3 min
03.03 · SQL

Electronics revenue

Return the total revenue (sum of total_price) from sales of products in the Electronics category, as one value.

3 min
03.04 · SQL

Stationery units

Return the total quantity_sold of products in the Stationery category (one value).

3 min
03.05 · SQL

List value vs what was paid

Some bills got a discount. For every sale return sale_id, product_name, list_value (= quantity_sold × the product's unit_price) and total_price (what was actually paid).

3 min
03.06 · SQL

Anything “Smart”

Return sale_id, product_name and total_price of the sales of products whose name contains Smart.

3 min
03.07 · SQL

How many at a time, for pricey items?

Return the average quantity_sold of sales of products whose unit_price is above ₹1,000, rounded to 2 decimal places (one value).

3 min
03.08 · SQL

Full receipt lines

Return sale_id, customer_name, product_name and sale_date for every sale, sorted by sale_id.

3 min
03.09 · SQL

Popularity count

For every product, including those never sold, return product_name and sales_count: how many sales it appears in (0 if none).

3 min
03.10 · SQL

Dead stock

Return product_id and product_name of the products that have never been sold.

3 min
03.11 · SQL

Window shoppers

Return customer_name and city of the customers who have never bought anything.

3 min
04

Groups, conditions & dates

HAVING, sorting by aggregates, conditional counts, months.

04.01 · Lesson

Groups, conditions and dates

GROUP BY after a join, HAVING, sorting by an aggregate, conditional counts and working with months.

3 min Free
04.02 · SQL

Revenue by product

For each product that has been sold, return product_name and total_revenue (the sum of its sales' total_price).

3 min
04.03 · SQL

Revenue by category

For each category that has sales, return category and total_revenue.

3 min
04.04 · SQL

Best seller by number of bills

Which product appears in the most sales? Return its product_id and sales_count (one row).

3 min
04.05 · SQL

The priciest aisle

Return the category with the highest average unit_price (just that one category name).

3 min
04.06 · SQL

Products that crossed ₹3,000

Return product_name of the products whose total revenue (sum of total_price over their sales) is more than ₹3,000.

3 min
04.07 · SQL

Sales per month

Return year, month (as numbers) and sales_count for every month that has sales, sorted by year, then month.

3 min
04.08 · SQL

Month labels

Return sale_month as text like 2024-01, and sales_count, for every month that has sales, sorted by sale_month.

3 min
04.09 · SQL

2024 revenue by category

Return category and total_revenue from sales made in the year 2024 only (categories with 2024 sales).

3 min
04.10 · SQL

Spending by city

Return city and total_spent (the sum of total_price of all sales to customers from that city), highest total first. Only cities whose customers bought something.

3 min
04.11 · SQL

Premium vs budget, per aisle

For every category return category, premium_count (products priced above ₹1,000) and budget_count (products priced ₹1,000 or less).

3 min
05

Subqueries & CASE

Queries inside queries, correlated subqueries and CASE.

05.01 · Lesson

Subqueries and CASE

Queries inside queries, correlated subqueries, CASE labels, date arithmetic and GREATEST.

3 min Free
05.02 · SQL

Above-average quantities

Return all columns of the sales whose quantity_sold is greater than the average quantity_sold of all sales.

3 min
05.03 · SQL

Above their own average

Return sale_id, customer_id and total_price of the sales worth more than that same customer's average sale.

3 min
05.04 · SQL

Fast movers

Return product_name, category and unit_price of the products whose total quantity sold (over all their sales) is greater than the average quantity_sold per sale.

3 min
05.05 · SQL

Bill size bands

For every sale return sale_id, total_price and bill_band:

3 min
05.06 · SQL

Days before quarter end

The quarter closes on 31 March 2024. For every sale return sale_id, sale_date and days_left: the number of days from the sale date to 31 March 2024 (a sale on 31 March would have 0).

3 min
05.07 · SQL

Weekday or weekend?

For every sale return sale_id, sale_date and day_type: Weekend if it was a Saturday or Sunday, otherwise Weekday.

3 min
05.08 · SQL

Electronics buyers

Return the customer_name of every customer who has bought at least one Electronics product, each name once.

3 min
05.09 · SQL

The biggest bill

Return all columns of the sale (or sales, if tied) with the highest total_price.

3 min
05.10 · SQL

Top product in each aisle

For each category return category, product_name and unit_price of its most expensive product.

3 min
05.11 · SQL

Minimum bill

The store has a minimum bill of ₹500. For every sale return sale_id, total_price and amount_billed: the larger of total_price and 500.

3 min
06

Window functions & CTEs

RANK, running totals, LAG, pivots and WITH.

06.01 · Lesson

Window functions and CTEs

Ranking, running totals, shares of a total, first/last per group, LAG, pivots and WITH.

3 min Free
06.02 · SQL

Revenue leaderboard

For each product that has been sold, return product_name, total_revenue and revenue_rank (1 = highest revenue, using RANK()), sorted by rank.

3 min
06.03 · SQL

Running total per category

For every sale return category, product_name, sale_date and running_total: the category's revenue from its first sale up to and including this sale's date.

3 min
06.04 · SQL

Share of revenue by category

For each category with sales return category, category_revenue and revenue_pct: its share of all revenue as a percentage, rounded to 2 decimals. Biggest share first.

3 min
06.05 · SQL

Top 3 revenue contributors

Return product_name, total_revenue and revenue_pct (share of all revenue, rounded to 2 decimals) for the 3 products with the biggest share, biggest first.

3 min
06.06 · SQL

First purchase of each customer

For each customer who bought something, return customer_name, product_name and sale_date of their earliest purchase.

3 min
06.07 · SQL

Latest purchase of each customer

For each customer who bought something, return customer_name, sale_date and total_price of their most recent sale.

3 min
06.08 · SQL

Quarter at a glance

Turn monthly revenue into columns. For each category with sales in January–March 2024, return category, jan, feb, mar: that category's revenue in each month (0 when it sold nothing that month).

3 min
06.09 · SQL

Price rank inside each aisle

For every product return category, product_name, unit_price and price_rank: its position by price within its own category (1 = most expensive), using DENSE_RANK().

3 min
06.10 · SQL

Month over month

Return sale_month (text like 2024-01), revenue (that month's total) and prev_revenue (the previous month's revenue; NULL for the first month), sorted by sale_month.

3 min
06.11 · SQL

Big spenders

Return customer_name and total_spent of the customers whose total spend is above the average total spend of customers who bought something.

3 min
07

Changing data & structure

INSERT, UPDATE, views, constraints, indexes, transactions, procedures.

07.01 · Lesson

Changing data and structure

INSERT, UPDATE, views, constraints, indexes, transactions and stored procedures.

3 min Free
07.02 · SQL

A name with an apostrophe

Add a new product: id 114, name Teacher's Diary, category Stationery, price ₹180.00.

3 min
07.03 · SQL

Clearance on the cheapest three

Cut the unit_price of the 3 cheapest products by 10%, rounded to 2 decimals. Leave every other product unchanged.

3 min
07.04 · SQL

A view of sales per product

Create a view named total_sales that shows, for each product that has been sold, product_name, category and total_sales_amount (the sum of its sales' total_price).

3 min
07.05 · SQL

A view of the top sellers

Create a view named top_products listing the 3 products with the most units sold: product_name and total_quantity_sold.

3 min
07.06 · SQL

A product dashboard view

Create a view named product_sales_info with, for every product (sold or not): product_id, product_name, category, unit_price and total_sales (the number of sales it appears in).

3 min
07.07 · SQL

No zero-quantity sales

Add a CHECK constraint named chk_quantity_sold to sales so that quantity_sold must always be greater than zero.

3 min
07.08 · SQL

Speeding up date lookups

Looking up a day's sales is the most common query at the counter. Create an index on sales(sale_date), then return all columns of the sales made on 9 March 2024.

3 min
07.09 · SQL

Sell and reprice, together

In one transaction:

3 min
07.10 · SQL

New product, first sale

In one transaction (both or neither):

3 min
07.11 · SQL

A price-update procedure

Create a stored procedure update_unit_price(p_product_id INT, p_new_price NUMERIC) that sets that product's unit_price to the new price.

3 min