SQL Fundamentals
Query and change relational data with SELECT, JOIN, GROUP BY, INSERT, UPDATE and DELETE, and understand transactions.
Beginner · about 10 hours · difficulty 2/5
An ORM writes SQL for you, but you still need to read and reason about SQL to model data, debug slow queries and pass technical interviews. Most placement tests include SQL questions.
Reading data
- SELECT with WHERE, ORDER BY, LIMIT and OFFSET.
- JOINs: INNER JOIN for matching rows, LEFT JOIN to keep rows with no match (students with zero applications).
- Aggregation: COUNT, SUM, AVG with GROUP BY and HAVING ("companies with more than 10 applicants").
- Subqueries and CTEs (WITH) to break complex queries into steps.
Changing data
INSERT, UPDATE and DELETE, always with a WHERE clause on updates and deletes. Use RETURNING in PostgreSQL to get the changed rows back.
Transactions
Group statements that must succeed or fail together (create an order and reduce stock) with BEGIN and COMMIT. Learn the ACID properties at a high level.
Practice setup
Install PostgreSQL locally or with Docker, or use a free hosted database, and connect with psql or a GUI like pgAdmin or DBeaver.
Pitfalls
- UPDATE without WHERE.
- Building SQL by concatenating strings with user input, which causes SQL injection. Always use parameterised queries.
- Using SELECT * everywhere in application code.
You are ready to move on when you can write a query that lists each company with its number of applicants and average applicant CGPA, including companies with zero applicants.
Key concepts
- SELECT, WHERE, ORDER BY
- INNER and LEFT JOIN
- GROUP BY and aggregates
- INSERT, UPDATE, DELETE
- Subqueries and CTEs
- Transactions and ACID
- Parameterised queries
- Window functions
Mini project
Create a students, companies and applications schema in PostgreSQL, load 100 rows of sample data, and write 15 queries answering placement-cell questions such as top recruiters, branch-wise placement rate and average CTC.
Check yourself
Why does an INNER JOIN between companies and applications drop companies with no applicants, and how do you keep them?
Free resources
- PostgreSQL Tutorial (PostgreSQL)
- SQLBolt interactive lessons (SQLBolt)
- SQL tutorial (W3Schools)