SQL Fundamentals
Query relational data with SELECT, WHERE, GROUP BY, HAVING, ORDER BY and joins, and understand how NULLs and data types behave.
Beginner · about 12 hours · difficulty 2/5
SQL is how you talk to databases and warehouses. Every data engineer interview in India starts with SQL, and most of your daily work ends in it.
The core query
SELECTcolumns and expressions,FROMa table,WHEREto filter rows.GROUP BYwith aggregates (COUNT,SUM,AVG,MIN,MAX),HAVINGto filter groups.ORDER BYandLIMIT.- Learn the logical order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT. It explains why you cannot use a SELECT alias in WHERE.
Joins
INNER,LEFT,RIGHT,FULL OUTERandCROSSjoins.- Joining on the right key: an order table joined to a payments table on
order_idcan multiply rows if an order has several payment attempts. - Anti-joins (
LEFT JOIN ... WHERE right.id IS NULLorNOT EXISTS) to find missing records.
NULLs and types
NULLmeans unknown.NULL = NULLis not true; useIS NULL.COUNT(col)skips NULLs,COUNT(*)does not.- Use
COALESCEdeliberately and know types: integers vs decimals for money,DATEvsTIMESTAMP, text.
DDL and DML
CREATE TABLE, INSERT, UPDATE, DELETE, primary and foreign keys. Practise on PostgreSQL locally or in Docker.
Pitfalls
- Using
DISTINCTto hide a join that duplicated rows instead of fixing the join. - Filtering a LEFT JOIN's right table in WHERE, which silently turns it into an INNER JOIN.
- Storing money as floats.
You are ready to move on when you can write, without help, a query returning each city's order count and revenue for last month, including cities with zero orders.
Key concepts
- SELECT, WHERE, ORDER BY, LIMIT
- GROUP BY and HAVING
- Logical query order
- Inner, left, full and cross joins
- Anti-joins and semi-joins
- NULL semantics
- Data types for money and time
- DDL and DML
Mini project
Load a sample e-commerce dataset into PostgreSQL. Write 15 queries of increasing difficulty: filters, aggregates, each join type, and an anti-join that finds customers who never ordered.
Check yourself
Why does a LEFT JOIN followed by WHERE payments.status = 'success' drop orders with no payments, and how do you fix it?
Free resources
- SQLBolt interactive lessons (SQLBolt)
- PostgreSQL tutorial (PostgreSQL)
- SQL roadmap (roadmap.sh)
- Mode SQL tutorial (Mode)