A subquery (or inner query) is a SQL query nested inside a larger query. The subquery is executed first, and its result is used by the outer query. Subqueries can appear in the WHERE
, SELECT
, FROM
, and HAVING
clauses.
You want to find all customers who have placed at least one order with a total amount greater than $100.
-
Connect to the MySQL Server:
First, connect to your MySQL server using the mysql
client. You'll need the username and password for your MySQL server. For this lab, let's assume you can connect as the root
user without a password.
Open your terminal and execute the following command:
mysql -u root
You should see the MySQL prompt: mysql>
.
-
Create the Database and Tables:
If you don't already have a database and tables, create them now. Let's create a database named labdb
and the customers
and orders
tables.
CREATE DATABASE IF NOT EXISTS labdb;
USE labdb;
CREATE TABLE IF NOT EXISTS customers (
customer_id INT PRIMARY KEY,
name VARCHAR(255),
city VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_amount DECIMAL(10, 2),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
-
Insert Sample Data:
Insert some sample data into the tables.
INSERT INTO customers (customer_id, name, city) VALUES
(1, 'Alice Smith', 'New York'),
(2, 'Bob Johnson', 'Los Angeles'),
(3, 'Charlie Brown', 'Chicago'),
(4, 'David Lee', 'Houston');
INSERT INTO orders (order_id, customer_id, order_date, total_amount) VALUES
(101, 1, '2023-01-15', 120.00),
(102, 2, '2023-02-20', 80.00),
(103, 1, '2023-03-10', 150.00),
(104, 3, '2023-04-05', 200.00),
(105, 2, '2023-05-12', 110.00),
(106, 4, '2023-06-18', 90.00);
-
Write the Subquery in the WHERE Clause:
Now, write the query to find customers who have placed orders with a total amount greater than $100.
SELECT *
FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders WHERE total_amount > 100);
Explanation:
- The subquery
(SELECT customer_id FROM orders WHERE total_amount > 100)
selects the customer_id
from the orders
table where the total_amount
is greater than 100.
- The outer query
SELECT * FROM customers WHERE customer_id IN (...)
selects all columns from the customers
table where the customer_id
is in the set of customer_id
s returned by the subquery.
-
Execute the Query:
Execute the query in your MySQL client. You should see the following output:
+-------------+-------------+-----------+
| customer_id | name | city |
+-------------+-------------+-----------+
| 1 | Alice Smith | New York |
| 2 | Bob Johnson | Los Angeles |
| 3 | Charlie Brown | Chicago |
+-------------+-------------+-----------+
3 rows in set (0.00 sec)
This shows that Alice Smith, Bob Johnson, and Charlie Brown have placed orders with a total amount greater than $100.
-
Alternative using EXISTS:
You can also achieve the same result using the EXISTS
operator. This approach can sometimes be more efficient, especially with larger datasets.
SELECT *
FROM customers c
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id AND o.total_amount > 100
);
This query checks if there exists at least one order for each customer with a total amount greater than 100.
-
Execute the EXISTS Query:
Execute the query in your MySQL client. You should see the same output as before:
+-------------+-------------+-----------+
| customer_id | name | city |
+-------------+-------------+-----------+
| 1 | Alice Smith | New York |
| 2 | Bob Johnson | Los Angeles |
| 3 | Charlie Brown | Chicago |
+-------------+-------------+-----------+
3 rows in set (0.00 sec)
-
Clean up (Optional):
If you want to clean up the database and tables, you can execute the following commands:
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS customers;
DROP DATABASE IF EXISTS labdb;
Remember to exit the MySQL client by typing exit
and pressing Enter.