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

  • SELECT columns and expressions, FROM a table, WHERE to filter rows.
  • GROUP BY with aggregates (COUNT, SUM, AVG, MIN, MAX), HAVING to filter groups.
  • ORDER BY and LIMIT.
  • 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 OUTER and CROSS joins.
  • Joining on the right key: an order table joined to a payments table on order_id can multiply rows if an order has several payment attempts.
  • Anti-joins (LEFT JOIN ... WHERE right.id IS NULL or NOT EXISTS) to find missing records.

NULLs and types

  • NULL means unknown. NULL = NULL is not true; use IS NULL. COUNT(col) skips NULLs, COUNT(*) does not.
  • Use COALESCE deliberately and know types: integers vs decimals for money, DATE vs TIMESTAMP, text.

DDL and DML

CREATE TABLE, INSERT, UPDATE, DELETE, primary and foreign keys. Practise on PostgreSQL locally or in Docker.

Pitfalls

  • Using DISTINCT to 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