quiz Databases · 10 questions

Advanced SQL Concepts

help_outline 10 questions
timer ~5 min
auto_awesome AI-generated
0 / 10
Score : 0%
1

Which clause must appear directly after FROM and before ORDER BY when grouping results?

2

If you need to list each distinct city from the customers table, which query is correct?

3

A query uses LEFT JOIN between orders and payments. What will be shown for orders without a matching payment?

4

Which aggregate function would you use to find the average salary of employees in each department?

5

Given the query: SELECT SUBSTRING(name, 1, 3) FROM users; what does it return?

6

When combining two tables with INNER JOIN, which rows are excluded from the result set?

7

Which statement correctly describes the purpose of a subquery in the WHERE clause?

8

If you execute SELECT COUNT(*) FROM orders WHERE amount > 1000, what does COUNT(*) count?

9

Which query correctly returns customers whose total purchase amount exceeds the overall average purchase amount?

10

When using GROUP BY with multiple columns, how does the grouping behave?

menu_book

Advanced SQL Concepts

Review key concepts before taking the quiz

Advanced SQL Concepts: A Comprehensive Guide

Welcome to this in‑depth Advanced SQL tutorial. Whether you are preparing for a certification exam or looking to sharpen your database skills, this course covers the most frequently tested concepts such as GROUP BY, DISTINCT, joins, aggregate functions, string manipulation, subqueries, and counting rows. Each section explains the theory, provides clear examples, and highlights common pitfalls to help you write efficient and error‑free queries.

1. Grouping Results with GROUP BY

The GROUP BY clause must appear directly after the FROM clause (and any WHERE filters) and before ORDER BY. It groups rows that share the same values in the specified columns, allowing aggregate functions to be applied to each group.

  • Syntax example:
    SELECT department_id, AVG(salary) AS avg_salary
    FROM employees
    WHERE hire_date > '2020-01-01'
    GROUP BY department_id
    ORDER BY avg_salary DESC;
  • Key points:
    • All columns in the SELECT list must either be aggregated (e.g., AVG, SUM) or appear in the GROUP BY clause.
    • Use HAVING to filter groups after aggregation.
    • Multiple columns can be grouped together for hierarchical grouping.

2. Selecting Unique Values with DISTINCT

When you need a list of unique entries—such as every distinct city in a customers table—DISTINCT is the most straightforward tool.

  • Correct query:
    SELECT DISTINCT city FROM customers;
  • Why not other options?
    • WHERE city IS NOT NULL removes NULL values but does not eliminate duplicates.
    • GROUP BY city also works, but DISTINCT is more concise for a single column.
    • ORDER BY city merely sorts the result; it does not guarantee uniqueness.
  • Performance tip: Use DISTINCT on indexed columns to reduce sorting overhead.

3. Understanding LEFT JOIN Behavior

A LEFT JOIN returns all rows from the left (first) table and the matching rows from the right table. If no match exists, the right‑hand columns are filled with NULL values.

  • Example scenario:
    SELECT o.order_id, p.payment_date
    FROM orders o
    LEFT JOIN payments p ON o.order_id = p.order_id;

    Orders without a corresponding payment will show NULL in the payment_date column.

  • Common misconception: A LEFT JOIN never raises an error because of missing matches; it simply supplies NULL placeholders.
  • Best practice: Always test for NULL when you need to identify unmatched rows, e.g., WHERE p.payment_id IS NULL to find orders awaiting payment.

4. Aggregate Functions: Calculating Averages

To compute the average salary per department, the AVG function is the appropriate choice.

  • Sample query:
    SELECT department_id, AVG(salary) AS avg_salary
    FROM employees
    GROUP BY department_id;
  • Other aggregates:
    • COUNT – counts rows.
    • SUM – adds numeric values.
    • MAX / MIN – find extreme values.
  • Tip for large tables: Use HAVING AVG(salary) > 50000 to filter departments after the average is calculated, reducing the amount of data transferred to the client.

5. String Manipulation with SUBSTRING

The SUBSTRING function extracts a portion of a string based on a start position and length. In the query SELECT SUBSTRING(name, 1, 3) FROM users;, the first three characters of each name are returned.

  • Syntax breakdown:
    • name – the source column.
    • 1 – starting index (SQL strings are 1‑based).
    • 3 – number of characters to return.
  • Example output:
    +-------------------+
    | SUBSTRING(name,1,3) |
    +-------------------+
    | Joh               |
    | Ann               |
    | Mar               |
    +-------------------+
  • Common error: Assuming four arguments are required; the three‑argument form is standard in most RDBMS (MySQL, PostgreSQL, SQL Server).

6. Inner Joins: Which Rows Are Excluded?

An INNER JOIN returns only rows that have matching keys in both tables. Any row lacking a counterpart is omitted from the result set.

  • Illustrative query:
    SELECT e.employee_id, d.department_name
    FROM employees e
    INNER JOIN departments d ON e.department_id = d.department_id;
  • Exclusion rule: If an employee belongs to a department that does not exist in the departments table, that employee will not appear in the output.
  • When to use: When you need only the intersecting data, such as orders that have both a customer and a shipping address.

7. Subqueries in the WHERE Clause

A subquery placed inside a WHERE clause provides a scalar (single) value that each row of the outer query can compare against.

  • Typical pattern:
    SELECT product_id, price
    FROM products
    WHERE price > (SELECT AVG(price) FROM products);

    This returns products priced above the overall average.

  • Why it matters: The subquery is executed once (or per row, depending on the optimizer) and its result is used as a constant for the comparison.
  • Alternative approaches: Use JOIN with derived tables for more complex filtering, but a scalar subquery remains the most readable for simple thresholds.

8. Counting Rows with COUNT(*)

The expression COUNT(*) counts every row that satisfies the WHERE condition, regardless of column values or NULL presence.

  • Example query:
    SELECT COUNT(*) AS high_value_orders
    FROM orders
    WHERE amount > 1000;

    The result is the total number of orders whose amount exceeds 1,000.

  • Difference from COUNT(column): COUNT(column) ignores rows where the specified column is NULL, while COUNT(*) never skips rows.
  • Performance tip: Ensure the WHERE predicate uses indexed columns to make the count operation fast on large tables.

9. Putting It All Together: A Mini Project

Below is a composite query that demonstrates several of the concepts covered:

SELECT c.city,
       COUNT(o.order_id) AS total_orders,
       AVG(o.amount) AS avg_order_amount,
       SUBSTRING(c.contact_name, 1, 3) AS name_prefix
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.amount > 1000
GROUP BY c.city, SUBSTRING(c.contact_name, 1, 3)
HAVING COUNT(o.order_id) > 5
ORDER BY total_orders DESC;

This query:

  • Uses LEFT JOIN to keep all cities, even those without qualifying orders.
  • Filters orders with amount > 1000 before aggregation.
  • Groups results by city and the first three letters of the contact name.
  • Applies HAVING to keep only cities with more than five high‑value orders.
  • Orders the final list by the number of orders, descending.

10. Quick Review Checklist

  • GROUP BY must follow FROM and precede ORDER BY.
  • DISTINCT returns unique rows; GROUP BY can achieve the same effect.
  • LEFT JOIN keeps all left‑hand rows and fills missing right‑hand columns with NULL.
  • INNER JOIN excludes rows without a matching key in the other table.
  • AVG calculates the mean; pair it with GROUP BY for per‑group averages.
  • SUBSTRING(name,1,3) extracts the first three characters of name.
  • Subquery in WHERE supplies a scalar value for comparison.
  • COUNT(*) counts every row that meets the WHERE clause.

By mastering these advanced SQL concepts, you will be equipped to write robust queries, optimize performance, and solve real‑world data challenges. Keep practicing with varied datasets, and refer back to this guide whenever you need a quick refresher.

Stop highlighting.
Start learning.

Join students who have already generated over 50,000 quizzes on Quizly. It's free to get started.