SQL Mastery Made Easy: 10 LeetCode-style Interview Questions Quiz

  1. Find Employees with High Salaries

    Given an Employee table with columns id, name, and salary, which SQL clause should be used to select employees earning more than 5000?

    1. A. WHERE salary u003E 5000
    2. B. HAVING salary u003E 5000
    3. C. SELECT salary u003E 5000
    4. D. ORDER BY salary u003E 5000
    5. E. GROUP BY salary u003E 5000
  2. Customers Without Orders

    Suppose you have Customers and Orders tables linked by customer_id. Which SQL operation will return all customers who never placed an order?

    1. A. LEFT JOIN with Orders and filter where Orders.customer_id IS NULL
    2. B. INNER JOIN on Orders by customer_id
    3. C. UNION of Customers and Orders
    4. D. SELECT Customers where id NOT IN Orders
    5. E. CROSS JOIN Customers and Orders
  3. Second Highest Salary

    Which SQL query finds the second-highest salary from the Employee table?

    1. A. SELECT MAX(salary) FROM Employee WHERE salary u003C (SELECT MAX(salary) FROM Employee)
    2. B. SELECT TOP 2 salary FROM Employee
    3. C. SELECT MIN(salary) FROM Employee
    4. D. SELECT salary FROM Employee LIMIT 2
    5. E. SELECT MAX(salary) FROM Employee
  4. Department with the Highest Average Salary

    Given Employee and Department tables, how do you find the department with the highest average salary?

    1. A. GROUP BY department and ORDER BY average salary DESC LIMIT 1
    2. B. GROUP BY department and ORDER BY MAX(salary)
    3. C. SELECT department WHERE AVG(salary)
    4. D. HAVING MAX(salary) FROM Department
    5. E. GROUP BY salary in Department
  5. Identifying Duplicate Emails

    In a Person table with an email column, which SQL is best to find all duplicate emails?

    1. A. SELECT email FROM Person GROUP BY email HAVING COUNT(email) u003E 1
    2. B. SELECT email FROM Person WHERE COUNT(*) u003E 1
    3. C. SELECT DISTINCT email FROM Person
    4. D. ORDER BY email FROM Person
    5. E. GROUP BY email FROM Person
  6. Ranking Employee Salaries

    If you want to assign a rank to each employee based on salary, giving equal salaries the same rank, which SQL window function would you use?

    1. A. RANK() OVER (ORDER BY salary DESC)
    2. B. ROWCOUNT() OVER (ORDER BY salary)
    3. C. COUNT() GROUP BY salary
    4. D. INDEX() FROM Employee
    5. E. COLLECT() OVER salary
  7. Employees Earning More Than Their Managers

    Which SQL clause helps you compare an employee's salary to their manager's salary when both are in the same Employee table?

    1. A. JOIN Employee as e ON manager_id = e.id
    2. B. SELECT DISTINCT salary FROM Employee
    3. C. GROUP BY manager_id
    4. D. WHERE salary u003E 1.1*manager_salary
    5. E. HAVING salary u003E MAX(salary)
  8. Finding the Median Salary

    What SQL window function can be used to help find the median salary from an Employee table?

    1. A. ROW_NUMBER() OVER (ORDER BY salary)
    2. B. SUM() OVER (partition BY salary)
    3. C. COUNT() OVER (ORDER BY salary)
    4. D. MIN() GROUP BY salary
    5. E. AVG(salary, median)
  9. GROUP BY Usage

    Which scenario best demonstrates when to use GROUP BY in SQL?

    1. A. When calculating the total salary per department
    2. B. When retrieving all columns without aggregation
    3. C. When filtering rows based on a value
    4. D. When updating salaries in bulk
    5. E. When deleting duplicate rows
  10. HAVING vs WHERE

    In SQL, when should you use HAVING instead of WHERE in a query?

    1. A. To filter results after aggregation, like selecting departments with average salary over 5000
    2. B. To filter individual rows before grouping
    3. C. To sort the query results by a column
    4. D. To join two unrelated tables
    5. E. To update a specific column's value