SQL Cheat Sheet

Keep this quick reference handy while you practice. The examples use a café menu and SQLite syntax.

Read columns and filter rows

SELECT name, price
FROM drinks
WHERE category = 'coffee' AND price >= 4;

SELECT chooses output columns, FROM names the source, and WHERE filters rows. Use single quotes for text values. To find missing values, use IS NULL.

Sort and limit results

SELECT name, price
FROM drinks
ORDER BY price DESC, name ASC
LIMIT 3;

DESC sorts highest first and ASC sorts lowest first. This query takes three rows after sorting. Names break price ties.

Count rows by category

SELECT category, COUNT(*) AS drink_count
FROM drinks
GROUP BY category
HAVING COUNT(*) >= 2;

GROUP BY creates one group per category. COUNT(*) counts its rows, and HAVING filters the resulting groups. Use WHERE when you need to filter individual rows before grouping.

Join two tables

SELECT d.name, s.supplier_name
FROM drinks AS d
JOIN suppliers AS s ON d.supplier_id = s.id;

This example assumes a suppliers table and a matching supplier_id in drinks. An inner JOIN returns matching rows. A LEFT JOIN also keeps drinks without a matching supplier.

Try your first interactive lesson

Practice selecting columns in your browser and get immediate feedback.

Start the free SQL lesson