Fatskills
Practice. Master. Repeat.
Study Guide: Data Analytics: SQL Fundamentals Subqueries
Source: https://www.fatskills.com/data-science/chapter/data-analytics-sql-fundamentals-subqueries

Data Analytics: SQL Fundamentals Subqueries

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~9 min read

What Is This?

A subquery is a query nested inside another query, used to retrieve data from a database based on conditions that depend on the results of the inner query. It's a powerful tool for complex data analysis and filtering.

This topic appears in exams and real-world tasks because it requires a deep understanding of database querying and data analysis, which are essential skills for any data professional. The examiner wants to test your ability to write efficient and effective queries, as well as your understanding of the underlying logic and grammar rules.

Why It Matters

Subqueries are tested in various exams, including database administration, data analysis, and software development certifications. They typically carry a significant number of marks, often between 20-40%. The examiner is testing your ability to:


  • Write efficient and effective queries
  • Understand the underlying logic and grammar rules
  • Apply subqueries in real-world scenarios
  • Identify and avoid common pitfalls and errors

Core Concepts

To master subqueries, you need to understand the following foundational ideas:


  • Correlated subqueries: These are subqueries that reference the outer query, often using variables or parameters.
  • Non-correlated subqueries: These are subqueries that do not reference the outer query, often used for filtering or aggregation.
  • Subquery syntax: You need to understand the syntax and grammar rules for writing subqueries, including the use of parentheses, keywords, and operators.
  • Subquery types: You need to understand the different types of subqueries, including IN, EXISTS, ANY, and ALL.

Prerequisites

Before tackling subqueries, you need to understand the following key concepts:


  • Basic SQL syntax and grammar
  • Database querying and data analysis
  • Data types and operators

If you're missing these prerequisites, you may struggle to understand the underlying logic and grammar rules of subqueries.

The Rule-Book (How It Works)

The primary rule for subqueries is:


  • Subqueries must be enclosed in parentheses: This is a fundamental syntax rule for writing subqueries.

Sub-rules and exceptions:


  • Correlated subqueries must reference the outer query: This means that the subquery must use variables or parameters from the outer query.
  • Non-correlated subqueries do not reference the outer query: This means that the subquery is self-contained and does not rely on the outer query.
  • Subqueries can be used in various clauses: Subqueries can be used in WHERE, FROM, JOIN, and HAVING clauses.

Visual pattern or mnemonic:


  • The "PAREN" rule: Remember that subqueries must be enclosed in parentheses, just like a parent's parentheses.

Exam / Job / Audit Weighting

Frequency: 20-40% Difficulty Rating: Intermediate Question Type or Real-World Task Type: Database querying and data analysis

Difficulty Level

Intermediate

Must-Know Rules, Formulas, Standards, or Principles

The following are the three most important rules and principles for subqueries:


  • Rule 1: Subqueries must be enclosed in parentheses: This is a fundamental syntax rule for writing subqueries.
  • Rule 2: Correlated subqueries must reference the outer query: This means that the subquery must use variables or parameters from the outer query.
  • Rule 3: Non-correlated subqueries do not reference the outer query: This means that the subquery is self-contained and does not rely on the outer query.

Worked Examples (Step-by-Step)

Here are three solved examples that escalate in difficulty:

Example 1: Simple subquery


SELECT * FROM customers
WHERE country IN (SELECT country FROM orders WHERE total > 1000);
  • The subquery selects the country from the orders table where the total is greater than 1000.
  • The outer query selects all customers from the customers table where the country is in the subquery results.

Example 2: Correlated subquery


SELECT * FROM customers
WHERE total_orders = (SELECT COUNT(*) FROM orders WHERE customer_id = customers.customer_id);
  • The subquery counts the number of orders for each customer.
  • The outer query selects all customers where the total number of orders is equal to the subquery result.

Example 3: Non-correlated subquery


SELECT * FROM customers
WHERE country IN (SELECT country FROM orders GROUP BY country HAVING COUNT(*) > 10);
  • The subquery groups the orders by country and counts the number of orders for each country.
  • The outer query selects all customers from the customers table where the country is in the subquery results.

Common Exam Traps & Mistakes

Here are four common errors that cost marks in exams:


  • Error 1: Missing parentheses: Forgetting to enclose the subquery in parentheses.
  • Error 2: Incorrect subquery syntax: Using the wrong syntax or grammar rules for writing subqueries.
  • Error 3: Correlated subquery error: Failing to reference the outer query in a correlated subquery.
  • Error 4: Non-correlated subquery error: Failing to use the correct syntax or grammar rules for a non-correlated subquery.

Shortcut Strategies & Exam Hacks

Here are some practical techniques to solve questions faster or more accurately under time pressure:


  • Use the "PAREN" rule: Remember that subqueries must be enclosed in parentheses.
  • Use the "CORR" rule: Remember that correlated subqueries must reference the outer query.
  • Use the "NON-CORR" rule: Remember that non-correlated subqueries do not reference the outer query.
  • Use the "GROUP BY" rule: Remember to group the subquery results if necessary.

Question-Type Taxonomy

Here are the three distinct question formats that subqueries appear in across different exams:


Format Description Example
Single subquery A single subquery is used to retrieve data from a database. SELECT * FROM customers WHERE country IN (SELECT country FROM orders WHERE total > 1000);
Correlated subquery A correlated subquery is used to retrieve data from a database based on conditions that depend on the results of the inner query. SELECT * FROM customers WHERE total_orders = (SELECT COUNT(*) FROM orders WHERE customer_id = customers.customer_id);
Non-correlated subquery A non-correlated subquery is used to retrieve data from a database based on conditions that do not depend on the results of the inner query. SELECT * FROM customers WHERE country IN (SELECT country FROM orders GROUP BY country HAVING COUNT(*) > 10);

Practice Set (MCQs)

Here are five multiple-choice questions at mixed difficulty levels:

Question 1: Easy
What is the purpose of a subquery? A) To retrieve data from a database B) To update data in a database C) To delete data from a database D) To create a new table in a database

Correct Answer: A) To retrieve data from a database Explanation: A subquery is used to retrieve data from a database based on conditions that depend on the results of the inner query.
Why the Distractors Are Tempting: The other options are plausible but incorrect, as subqueries are not used for updating, deleting, or creating data.

Question 2: Medium
What is the difference between a correlated subquery and a non-correlated subquery? A) Correlated subqueries reference the outer query, while non-correlated subqueries do not.
B) Correlated subqueries do not reference the outer query, while non-correlated subqueries do.
C) Correlated subqueries are used for filtering, while non-correlated subqueries are used for aggregation.
D) Correlated subqueries are used for aggregation, while non-correlated subqueries are used for filtering.

Correct Answer: A) Correlated subqueries reference the outer query, while non-correlated subqueries do not.
Explanation: Correlated subqueries reference the outer query, while non-correlated subqueries do not.
Why the Distractors Are Tempting: The other options are plausible but incorrect, as correlated subqueries reference the outer query, while non-correlated subqueries do not.

Question 3: Hard
What is the purpose of the GROUP BY clause in a subquery? A) To group the subquery results by a specific column B) To filter the subquery results based on a specific condition C) To aggregate the subquery results using a specific function D) To create a new table in the database

Correct Answer: A) To group the subquery results by a specific column Explanation: The GROUP BY clause is used to group the subquery results by a specific column, such as country or region.
Why the Distractors Are Tempting: The other options are plausible but incorrect, as the GROUP BY clause is used for grouping, not filtering or aggregating.

Question 4: Easy
What is the correct syntax for a subquery? A) SELECT * FROM customers WHERE country IN (SELECT country FROM orders); B) SELECT * FROM customers WHERE country IN (SELECT country FROM orders WHERE total > 1000); C) SELECT * FROM customers WHERE country IN (SELECT country FROM orders GROUP BY country); D) SELECT * FROM customers WHERE country IN (SELECT country FROM orders HAVING COUNT(*) > 10);

Correct Answer: B) SELECT * FROM customers WHERE country IN (SELECT country FROM orders WHERE total > 1000); Explanation: The correct syntax for a subquery is to enclose the subquery in parentheses and use the IN keyword to specify the condition.
Why the Distractors Are Tempting: The other options are plausible but incorrect, as they do not use the correct syntax or grammar rules for writing subqueries.

Question 5: Medium
What is the purpose of the EXISTS keyword in a subquery? A) To retrieve data from a database based on the existence of a specific condition B) To update data in a database based on the existence of a specific condition C) To delete data from a database based on the existence of a specific condition D) To create a new table in the database based on the existence of a specific condition

Correct Answer: A) To retrieve data from a database based on the existence of a specific condition Explanation: The EXISTS keyword is used to retrieve data from a database based on the existence of a specific condition, such as the existence of a specific record or value.
Why the Distractors Are Tempting: The other options are plausible but incorrect, as the EXISTS keyword is used for retrieving data, not updating, deleting, or creating data.

30-Second Cheat Sheet

Here are the five key things to remember when it comes to subqueries:


  • Subqueries must be enclosed in parentheses: This is a fundamental syntax rule for writing subqueries.
  • Correlated subqueries must reference the outer query: This means that the subquery must use variables or parameters from the outer query.
  • Non-correlated subqueries do not reference the outer query: This means that the subquery is self-contained and does not rely on the outer query.
  • Subqueries can be used in various clauses: Subqueries can be used in WHERE, FROM, JOIN, and HAVING clauses.
  • Use the "PAREN" rule: Remember that subqueries must be enclosed in parentheses.

Learning Path

Here is a suggested study sequence to master subqueries from scratch to exam-ready:


  • Beginner foundation: Understand the basics of SQL syntax and grammar, including the use of parentheses and keywords.
  • Core rules: Learn the core rules and principles of subqueries, including the use of correlated and non-correlated subqueries.
  • Practice: Practice writing subqueries using various clauses and conditions.
  • Timed drills: Practice writing subqueries under timed conditions to improve your speed and accuracy.
  • Mock tests: Take mock tests to assess your knowledge and identify areas for improvement.

Related Topics

Here are three closely connected topics that appear alongside subqueries in exams:


  • SQL syntax and grammar: Understanding the basics of SQL syntax and grammar is essential for writing subqueries.
  • Database querying and data analysis: Subqueries are used to retrieve data from a database based on conditions that depend on the results of the inner query.
  • Data types and operators: Understanding data types and operators is essential for writing subqueries that use conditions and filters.


ADVERTISEMENT