Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
KevsRobots Learning Platform
78% Percent Complete
By Kevin McAleer, 6 Minutes
In this lesson, we’ll explore subqueries (also known as nested queries), which allow you to perform queries within queries. Subqueries are powerful tools for retrieving data based on the results of other queries, enabling more advanced data analysis and manipulation.
A subquery is a query nested within another SQL query. Subqueries are typically enclosed in parentheses and can appear in various parts of the main query, including the SELECT, FROM, and WHERE clauses.
SELECT
FROM
WHERE
Subqueries allow you to:
Subqueries can be categorized based on how and where they’re used:
Subqueries are often used in the WHERE clause to filter results based on the output of another query.
Suppose we have two tables, employees and departments.
employees
departments
| emp_id | name | salary | dept_id | |--------|----------|--------|---------| | 1 | Alice | 50000 | 1 | | 2 | Bob | 60000 | 2 | | 3 | Charlie | 70000 | 1 |
| dept_id | dept_name | |---------|---------------| | 1 | Engineering | | 2 | Sales |
Example: Find employees who earn more than the average salary in the employees table.
SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
In this example, the subquery (SELECT AVG(salary) FROM employees) returns a single value (the average salary), which is then used as a condition in the WHERE clause.
(SELECT AVG(salary) FROM employees)
Subqueries can also be used in the SELECT clause to calculate values for each row in the result set.
Example: List each employee’s salary and the average salary of their department.
SELECT name, salary, (SELECT AVG(salary) FROM employees AS e WHERE e.dept_id = employees.dept_id) AS dept_avg_salary FROM employees;
In this example, the subquery calculates the average salary for the department of each employee and displays it as dept_avg_salary.
dept_avg_salary
Subqueries can be used in the FROM clause to create a temporary table that the main query can then use.
Example: Find the highest salary by department.
SELECT dept_name, MAX(salary) AS max_salary FROM (SELECT departments.dept_name, employees.salary FROM employees JOIN departments ON employees.dept_id = departments.dept_id) AS dept_salaries GROUP BY dept_name;
In this example, the subquery creates a temporary table dept_salaries that includes both department names and salaries. The main query then groups this temporary table by dept_name and finds the maximum salary for each department.
dept_salaries
dept_name
A correlated subquery depends on the outer query for its values and is executed once for each row processed by the outer query. These are useful for filtering or calculating data that depends on row-by-row conditions.
Example: Find employees whose salary is above the average salary in their department.
SELECT name, salary FROM employees AS e1 WHERE salary > (SELECT AVG(salary) FROM employees AS e2 WHERE e1.dept_id = e2.dept_id);
In this example, the subquery calculates the average salary for each department. The outer query then retrieves employees whose salary is above the department average.
IN
The IN operator is commonly used with subqueries to filter results based on a list of values returned by the subquery.
Example: Find employees who work in departments with an average salary greater than $60,000.
SELECT name FROM employees WHERE dept_id IN (SELECT dept_id FROM employees GROUP BY dept_id HAVING AVG(salary) > 60000);
In this example, the subquery returns dept_id values where the department’s average salary exceeds $60,000. The outer query then retrieves employees who work in these departments.
dept_id
Filter with a Subquery: Retrieve employees with a salary greater than the average salary.
Correlated Subquery: Find employees who earn more than the average salary of their department.
Subquery with IN: Find employees working in departments with an average salary above $50,000.
SELECT name FROM employees WHERE dept_id IN (SELECT dept_id FROM employees GROUP BY dept_id HAVING AVG(salary) > 50000);
Subquery in SELECT: Display each employee’s name, salary, and the average salary of their department.
Here’s a summary of the various ways subqueries can be used in SQL:
WHERE salary > (SELECT AVG(salary) ...)
SELECT name, (SELECT AVG(salary) ...) AS avg
FROM (SELECT dept_name, salary ...) AS temp
Subqueries add powerful functionality to SQL queries, allowing for complex filtering and data manipulation. In the next lesson, we’ll cover views and indexes, which enhance the performance and usability of your database.
< Previous Next >