cswork / libraryOpen practice workspace

Coding questions · Amazon

Identify SQL Joins and Correct Query Errors

This question evaluates relational database fundamentals including the role of primary keys, SQL join types, set operations (UNION vs UNION ALL), date functions, and the ability to debug and correct SQL query syntax, reflecting both conceptual understanding and practical query-writing skills.

Winner

+----+-------+
| ID | Name  |
+----+-------+
| 1  | Alice |
| 2  | Bob   |
| 3  | Carol |
+----+-------+

Loser

+----+-------+
| ID | Name  |
+----+-------+
| 4  | Dave  |
| 5  | Erin  |
| 6  | Frank |
+----+-------+
Scenario

Database fundamentals and querying names from separate winner and loser tables

Question

What is a primary key and why is it important in relational databases? List the different types of SQL joins you know and explain when you would use each. The following query has errors. Identify and correct them:

select name MONTH(timestamp)

FROM table

WHEREMONTH(timestamp)>2010 Given tables Winner(ID, Name) and Loser(ID, Name), write a query that outputs all names in a single column. Use both UNION and UNION ALL, and explain the difference between the two operators.

Hints

Review relational keys, join semantics, date functions, UNION vs UNION ALL, and standard SQL syntax corrections.

Verify Primary Key Uniqueness

Show that Winner.ID (the primary key) is unique by returning the total number of rows and the count of distinct IDs from the Winner table.

Table Winner

columntypeconstraints
IDINTEGERPRIMARY KEY, NOT NULL
NameVARCHAR(50)NOT NULL

Sample data:

IDName
1Alice
2Bob
3Carol

Expected output

The counts should match (3 and 3), demonstrating the uniqueness of the primary key Winner.ID.

distinct_idstotal_rows
33

Hints

  1. Use COUNT(*) and COUNT(DISTINCT ID) in the same SELECT.
  2. If the two counts are equal, the primary key column has no duplicates.

List All Winner-Loser Pairs

Return all possible pairs of Winner and Loser names using a CROSS JOIN. Output columns as WinnerName and LoserName, ordered alphabetically by WinnerName and then LoserName.

Table Winner

columntypeconstraints
IDINTEGERPRIMARY KEY, NOT NULL
NameVARCHAR(50)NOT NULL

Sample data:

IDName
1Alice
2Bob
3Carol

Table Loser

columntypeconstraints
IDINTEGERPRIMARY KEY, NOT NULL
NameVARCHAR(50)NOT NULL

Sample data:

IDName
4Dave
5Erin
6Frank

Expected output

All 3x3 combinations (9 rows) of Winner and Loser names.

LoserNameWinnerName
DaveAlice
ErinAlice
FrankAlice
DaveBob
ErinBob
FrankBob
DaveCarol
ErinCarol
FrankCarol

Hints

  1. Use CROSS JOIN to produce the Cartesian product of the two tables.
  2. Order by both WinnerName and LoserName for deterministic output.

Fix Function and WHERE Syntax

Correct the erroneous query "select Name LENGTH(Name) FROM Winner WHERELENGTH(Name) > 3" so that it properly lists each Winner name with its character length where the length is greater than 3.

Table Winner

columntypeconstraints
IDINTEGERPRIMARY KEY, NOT NULL
NameVARCHAR(50)NOT NULL

Sample data:

IDName
1Alice
2Bob
3Carol

Expected output

Names from Winner with more than 3 characters and their lengths.

NameNameLength
Alice5
Carol5

Hints

  1. Add a comma between expressions in the SELECT list.
  2. Put a space after WHERE and alias the computed column.
  3. Use LENGTH(Name) in both the SELECT list and WHERE clause.

Combine Names With UNION and UNION ALL

Write two queries that output a single-column list of all names from Winner and Loser: one using UNION and one using UNION ALL. Order by Name for deterministic results and be prepared to explain the difference between the two set operators.

Table Winner

columntypeconstraints
IDINTEGERPRIMARY KEY, NOT NULL
NameVARCHAR(50)NOT NULL

Sample data:

IDName
1Alice
2Bob
3Carol

Table Loser

columntypeconstraints
IDINTEGERPRIMARY KEY, NOT NULL
NameVARCHAR(50)NOT NULL

Sample data:

IDName
4Dave
5Erin
6Frank

Expected output

A single-column list of all names from both tables. UNION removes duplicates; UNION ALL keeps them. With the provided data (no overlapping names), both queries return the same six names.

Name
Alice
Bob
Carol
Dave
Erin
Frank

Hints

  1. UNION removes duplicate rows, whereas UNION ALL preserves them.
  2. Both SELECT statements combined by UNION or UNION ALL must have the same number of columns and compatible data types.
  3. Use ORDER BY Name at the end of each combined query for deterministic output.