SQL Joins SQL Topic exerciseEasyVerified answerSQLite + PostgreSQL + MySQL live · 2 guided

USING — Compact Equality Join on Same-Named Columns

Join employees to departments with USING(department_id) and return the requested directory columns.

  • Joins
  • Sorting

Exercise brief

Understand the request

Data platform reviewer A portability review compares compact shared-key syntax with explicit ON predicates.

When the join key has the SAME name on both sides, the USING(col) clause is a compact alternative to ON. Re-do Q1 (employees + departments) using JOIN ... USING(department_id). Return department_id, employee_id, first_name, last_name, department_name — ordered by employee_id.

Return

  • Return department_id, employee_id, first_name, last_name, department_name in this exact left-to-right order.

Constraints

  • Use JOIN ... USING(department_id).
  • Do not use NATURAL JOIN because schema changes could alter its predicate silently.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • first_nameVARCHAR(50)
  • last_nameVARCHAR(50)
  • department_idINTEGER

departments

  • department_idINTEGER
  • department_nameVARCHAR(50)

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

USING (col) is shorthand for ON left.col = right.col when both sides spell the column the same.

Hint 2

Inside the SELECT, reference the USING column WITHOUT a table prefix — there is only one of it after the join.

Hint 3

NATURAL JOIN goes further (joins on EVERY same-named column) but is dangerous — schema changes silently change the join.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT department_id, e.employee_id, e.first_name, e.last_name, d.department_name FROM employees e INNER JOIN departments d USING (department_id) ORDER BY e.employee_id;

Why this works

USING is purely syntactic sugar for ON, with one twist: the joined column appears once (and unqualified) in the result. PostgreSQL, SQLite, MySQL all support it. SQL Server does NOT — use ON there.

Success check

The result matches the equality join while demonstrating shared-key USING syntax.

Expected result

Use this output to verify values, aliases, ordering, and row count.

department_idemployee_idfirst_namelast_namedepartment_name
10100JohnSmithIT
10101AliceJohnsonIT
10102BobWilsonIT
20103CarolDavisHR
30104DavidBrownFinance
40105EmmaTaylorMarketing
40106FrankGreenMarketing
10107GraceWhiteIT
20108HenryClarkHR
30109IvyMartinezFinance

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.