SQL Queries and Joins

SQL retrieves data declaratively; joins combine rows from multiple tables on matching columns, and GROUP BY with aggregate functions summarises groups, with WHERE filtering rows before grouping and HAVING filtering groups after.

Key formulas & points

Skim these first — then read the full notes below.

  • WHERE filters rows before grouping; HAVING filters after
  • Aggregates: COUNT, SUM, AVG, MIN, MAX
  • A subquery and a join can often be rewritten as each other

Topic details

Introduction

This topic covers querying relational data. You write joins (inner and outer), aggregate with GROUP BY, apply WHERE versus HAVING correctly, and use subqueries — understanding the logical order in which SQL clauses are evaluated.

Key relations & formulas

Formulas (Indian textbook notation)

  • SELECTcolsFROMRJOINSONR.a=S.bSELECT cols FROM R JOIN S ON R.a = S.b

Formulas (Indian textbook notation)

  • INNERJOINkeepsmatches;LEFTJOINkeepsallofRplusmatchedSINNER JOIN keeps matches; LEFT JOIN keeps all of R plus matched S

Formulas (Indian textbook notation)

  • GROUPBYcolHAVINGconditionfiltersafteraggregationGROUP BY col HAVING condition filters after aggregation

Notation and sign conventions

Relation 1 —
SELECTcolsFROMRJOINSONR.a=S.bSELECT cols FROM R JOIN S ON R.a = S.b

Formulas (Indian textbook notation)

  • SELECTcolsFROMRJOINSONR.a=S.bSELECT cols FROM R JOIN S ON R.a = S.b
Write this relation with symbols exactly as in Database System Concepts — Korth, Silberschatz & Sudarshan before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
INNERJOINkeepsmatches;LEFTJOINkeepsallofRplusmatchedSINNER JOIN keeps matches; LEFT JOIN keeps all of R plus matched S

Formulas (Indian textbook notation)

  • INNERJOINkeepsmatches;LEFTJOINkeepsallofRplusmatchedSINNER JOIN keeps matches; LEFT JOIN keeps all of R plus matched S
Write this relation with symbols exactly as in Database System Concepts — Korth, Silberschatz & Sudarshan before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
GROUPBYcolHAVINGconditionfiltersafteraggregationGROUP BY col HAVING condition filters after aggregation

Formulas (Indian textbook notation)

  • GROUPBYcolHAVINGconditionfiltersafteraggregationGROUP BY col HAVING condition filters after aggregation
Write this relation with symbols exactly as in Database System Concepts — Korth, Silberschatz & Sudarshan before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

SQL is declarative: you state what you want and the optimiser decides how. The key to correct queries is the logical evaluation order — FROM and JOIN first build the combined rows, WHERE filters individual rows, GROUP BY forms groups, HAVING filters those groups, and only then SELECT projects columns. That is why an aggregate condition belongs in HAVING (which runs after grouping) and a row condition in WHERE (which runs before). Join type matters too: an inner join drops unmatched rows, while a left outer join preserves every left row and fills missing right columns with nulls, which changes counts and aggregates significantly.

Assumptions and validity limits

State assumptions explicitly before using any relation for sql queries and joins — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Database Systems viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Database Systems papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to sql queries and joins.
4. Use equation 1:
SELECTcolsFROMRJOINSONR.a=S.bSELECT cols FROM R JOIN S ON R.a = S.b
.
5. Use equation 2:
INNERJOINkeepsmatches;LEFTJOINkeepsallofRplusmatchedSINNER JOIN keeps matches; LEFT JOIN keeps all of R plus matched S
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

SQL Queries and Joins appears in enterprise applications. In Indian it software curricula this topic is tested because it connects theory to relational model and SQL.
GATE and semester exams often combine sql queries and joins with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use sql queries and joins?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students put aggregate conditions in WHERE instead of HAVING, forget that INNER JOIN silently drops unmatched rows, and mishandle NULLs in outer-join results and aggregates. Misremembering the clause evaluation order leads to invalid column references.

Quick revision checklist

Before attempting sql queries and joins problems, confirm you can:
1. WHERE filters rows before grouping; HAVING filters after
2. Aggregates: COUNT, SUM, AVG, MIN, MAX
3. A subquery and a join can often be rewritten as each other
Revise the solved examples in Database System Concepts — Korth, Silberschatz & Sudarshan and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

GROUP BY aggregate

Problem

Department A has salaries 20k, 25k and 30k. Write the query result for average salary by department.

Solution

SELECT dept, AVG(salary) FROM emp GROUP BY dept. For A, AVG = (20 + 25 + 30)/3 = 25k. GROUP BY forms one group per department, then AVG summarises it.

Conceptual check — SQL Queries and Joins

Problem

In a Database Systems semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of sql queries and joins." What should a complete answer include?

Exams & GATE

Practice nested queries and GROUP BY with HAVING for GATE DB.

📖 Standard books (India)

  • Database System ConceptsKorth, Silberschatz & Sudarshan

    Read: Syllabus unit

    SQL, normalization, and transactions