0
0
Intro to Computingfundamentals~20 mins

SQL as the query language in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SQL Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🔍 Analysis
intermediate
2:00remaining
What is the output of this SQL query?

Consider a table Employees with columns id, name, and salary. The table has the following rows:

  • (1, 'Alice', 5000)
  • (2, 'Bob', 7000)
  • (3, 'Charlie', 7000)
  • (4, 'Diana', 6000)

What will this query return?

SELECT name FROM Employees WHERE salary = (SELECT MAX(salary) FROM Employees);
Intro to Computing
SELECT name FROM Employees WHERE salary = (SELECT MAX(salary) FROM Employees);
A
Bob
Charlie
B
Alice
Diana
CBob
DCharlie
Attempts:
2 left
💡 Hint

Think about which salary is the highest and which employees have that salary.

🧠 Conceptual
intermediate
1:30remaining
Which SQL clause filters rows after grouping?

After grouping rows with GROUP BY, which SQL clause is used to filter groups based on a condition?

AHAVING
BWHERE
CORDER BY
DFROM
Attempts:
2 left
💡 Hint

Remember, WHERE filters rows before grouping.

trace
advanced
2:30remaining
Trace the output of this SQL query with JOIN

Given two tables:

Students: (id, name)

  • (1, 'Anna')
  • (2, 'Ben')
  • (3, 'Cara')

Scores: (student_id, score)

  • (1, 85)
  • (2, 90)
  • (2, 95)
  • (4, 80)

What is the output of this query?

SELECT Students.name, Scores.score FROM Students LEFT JOIN Scores ON Students.id = Scores.student_id ORDER BY Students.id, Scores.score DESC;
Intro to Computing
SELECT Students.name, Scores.score FROM Students LEFT JOIN Scores ON Students.id = Scores.student_id ORDER BY Students.id, Scores.score DESC;
A
Anna NULL
Ben 90
Ben 95
Cara 80
B
Anna 85
Ben 90
Cara NULL
C
Anna 85
Ben 90
Ben 95
Cara NULL
D
Anna 85
Ben 95
Ben 90
Cara NULL
Attempts:
2 left
💡 Hint

LEFT JOIN keeps all rows from Students even if no matching Scores exist.

identification
advanced
1:30remaining
Identify the error in this SQL query

What error will this query produce?

SELECT name, COUNT(*) FROM Employees;
ATypeError: name column not found
BSyntaxError: Missing GROUP BY clause
CRuntimeError: COUNT(*) cannot be used with other columns
DNo error, query runs successfully
Attempts:
2 left
💡 Hint

When using aggregation functions with other columns, what else is required?

Comparison
expert
3:00remaining
Compare the results of these two queries

Given a table Orders with columns order_id, customer_id, and amount, which statement is true?

Query 1: SELECT customer_id, SUM(amount) FROM Orders GROUP BY customer_id HAVING SUM(amount) > 1000;
Query 2: SELECT customer_id, SUM(amount) FROM Orders WHERE amount > 1000 GROUP BY customer_id;
AQuery 1 filters rows with amount > 1000; Query 2 filters groups with total amount > 1000
BBoth queries return the same results
CQuery 1 filters groups with total amount > 1000; Query 2 filters rows with amount > 1000 before grouping
DQuery 1 will cause a syntax error; Query 2 runs correctly
Attempts:
2 left
💡 Hint

Consider when filtering happens in each query.