Databases

SQL Tutorial for Beginners: Real Queries for Real Jobs

10 min read Updated June 2026

Every analyst, every backend developer, every data engineer writes SQL. This tutorial walks you from your first SELECT to window functions in one sitting, using a single example schema you can keep in your head.

01.Our example schema

Imagine an online store with three tables. We'll use this throughout.

customers(id, name, city)
orders(id, customer_id, total, created_at)
order_items(id, order_id, product, qty, price)

02.SELECT, WHERE, ORDER BY

The three clauses cover the majority of analyst queries. Always reach for `LIMIT` when you're exploring a new table — production databases punish people who forget.

SELECT name, city
FROM customers
WHERE city = 'Chennai'
ORDER BY name
LIMIT 50;

03.JOINs without the confusion

INNER JOIN keeps only matches. LEFT JOIN keeps everything from the left table. RIGHT JOIN is rarely used in practice — flip the tables and use LEFT instead.

SELECT c.name, COUNT(o.id) AS order_count
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.name
ORDER BY order_count DESC;

04.GROUP BY and aggregates

SUM, COUNT, AVG, MIN, MAX. Always include every non-aggregated column in GROUP BY. PostgreSQL will refuse to run the query otherwise — that's a feature, not a bug.

05.Window functions for ranking

Window functions compute a value across a set of rows without collapsing them. Useful for ranking, running totals and de-duplication.

SELECT id, customer_id, total,
       ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) AS recency
FROM orders;

06.EXPLAIN: read the query plan

When a query is slow, prefix it with `EXPLAIN ANALYZE`. Look for `Seq Scan` on big tables (usually bad) and missing indexes. Adding the right index is often a 100x speedup.

Take Databases from tutorial to job offer.

Our Databases programs come with projects, mentor reviews and 100% placement support.