0% found this document useful (0 votes)
0 views

SQL Query Practice

The document contains a series of SQL query exercises and solutions, covering simple to complex queries involving tables like 'orders', 'employees', 'customers', and 'products'. It includes explanations for each query, detailing how they retrieve specific data based on given conditions. Additionally, it discusses the use of GROUP BY and COUNT functions in SQL for data aggregation and filtering.

Uploaded by

Nikhil Kumawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

SQL Query Practice

The document contains a series of SQL query exercises and solutions, covering simple to complex queries involving tables like 'orders', 'employees', 'customers', and 'products'. It includes explanations for each query, detailing how they retrieve specific data based on given conditions. Additionally, it discusses the use of GROUP BY and COUNT functions in SQL for data aggregation and filtering.

Uploaded by

Nikhil Kumawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL Query Practice

Simple SQL Queries


1. Suppose you have a table called "orders" with columns "order_id" (primary key),
"customer_id", "order_date", and "total_price". Write an SQL query to retrieve all orders made
by customer with ID 100.
2. Consider a table called "employees" with columns "emp_id" (primary key), "name", "dept_id",
and "salary". Write an SQL query to retrieve the names and salaries of all employees in the
department with ID 10.
3. Assume you have a table called "products" with columns "product_id" (primary key), "name",
"price", and "category". Write an SQL query to retrieve the names and prices of all products in
the "Electronics" category.
4. Suppose you have two tables called "customers" and "orders". The "customers" table has
columns "customer_id" (primary key), "name", and "email". The "orders" table has columns
"order_id" (primary key), "customer_id", "order_date", and "total_price". Write an SQL query to
retrieve the names and email addresses of all customers who have made an order.
5. Consider a table called "employees" with columns "emp_id" (primary key), "name", "dept_id",
and "salary". Write an SQL query to retrieve the name, salary, and department name of all
employees in the department with ID 10.
6. Assume you have a table called "orders" with columns "order_id" (primary key), "customer_id",
"order_date", and "total_price". Write an SQL query to retrieve the total price of all orders
made in the month of January 2022.
7. Suppose you have a table called "customers" with columns "customer_id" (primary key),
"name", and "email". Write an SQL query to retrieve the names and email addresses of all
customers whose email address contains the word "gmail".
8. Consider a table called "employees" with columns "emp_id" (primary key), "name", "dept_id",
and "salary". Write an SQL query to retrieve the names and salaries of all employees who earn
more than $50,000 per year.
9. Assume you have a table called "orders" with columns "order_id" (primary key), "customer_id",
"order_date", and "total_price". Write an SQL query to retrieve the total price of all orders
made by customers whose ID is not equal to 100.
10. Suppose you have two tables called "customers" and "orders". The "customers" table has
columns "customer_id" (primary key), "name", and "email". The "orders" table has columns
"order_id" (primary key), "customer_id", "order_date", and "total_price". Write an SQL query to
retrieve the names and email addresses of all customers who have not made an order.

Certainly! Here is the table with solutions to each question:

Question SQL Query

1 SELECT * FROM orders WHERE customer_id = 100;


Question SQL Query

2 SELECT name, salary FROM employees WHERE dept_id = 10;

3 SELECT name, price FROM products WHERE category = 'Electronics';

SELECT customers.name, customers.email FROM customers JOIN orders ON


4
customers.customer_id = orders.customer_id;

SELECT employees.name, employees.salary, departments.name FROM employees


5 JOIN departments ON employees.dept_id = departments.dept_id WHERE
departments.dept_id = 10;

SELECT SUM(total_price) FROM orders WHERE order_date >= '2022-01-01' AND


6
order_date < '2022-02-01';

7 SELECT name, email FROM customers WHERE email LIKE '%gmail%';

8 SELECT name, salary FROM employees WHERE salary > 50000;

9 SELECT SUM(total_price) FROM orders WHERE customer_id <> 100;

SELECT customers.name, customers.email FROM customers LEFT JOIN orders ON


10
customers.customer_id = orders.customer_id WHERE orders.order_id IS NULL;

Explanation:

1. The query selects all columns from the "orders" table where the "customer_id" column is
equal to 100.
2. The query selects the "name" and "salary" columns from the "employees" table where the
"dept_id" column is equal to 10.
3. The query selects the "name" and "price" columns from the "products" table where the
"category" column is equal to 'Electronics'.
4. The query selects the "name" and "email" columns from the "customers" table where there is
a match in the "orders" table on the "customer_id" column.
5. The query selects the "name", "salary", and department name from the "employees" table
where the "dept_id" column is equal to 10 and joins with the "departments" table on the
"dept_id" column.
6. The query selects the sum of the "total_price" column from the "orders" table where the
"order_date" column is between January 1st, 2022 and January 31st, 2022.
7. The query selects the "name" and "email" columns from the "customers" table where the
"email" column contains the word "gmail".
8. The query selects the "name" and "salary" columns from the "employees" table where the
"salary" column is greater than 50,000.
9. The query selects the sum of the "total_price" column from the "orders" table where the
"customer_id" column is not equal to 100.
10. The query selects the "name" and "email" columns from the "customers" table where there is
no match in the "orders" table on the "customer_id" column. This is done by performing a
LEFT JOIN of the "customers" table with the "orders" table and selecting rows where there is
no matching "order_id" in the "orders" table.
Can we solve this question using the group by query Assume you have a table called "products"
with columns "product_id" (primary key), "name", "price", and "category". Write an SQL query to
retrieve the names and prices of all products in the "Electronics" category.

Yes, we can solve this question using a GROUP BY query.

Here's the SQL query:

SELECT name, MAX(price) as price language-sql


FROM products
WHERE category = 'Electronics'
GROUP BY name;

This query selects the "name" and maximum "price" of all products in the "Electronics" category
from the "products" table, and groups the results by the "name" column.

The MAX() function is used to retrieve the maximum value of "price" for each unique "name" in the
"Electronics" category.

Note that the GROUP BY clause is necessary to ensure that we get only one row per product name,
even if there are multiple rows for the same product name and category.

Complex SQL Queries - Necessary


Sure, here are some more intermediate level SQL query questions that involve the concepts you
mentioned:

1. Suppose you have two tables called "employees" and "departments". The "employees" table
has columns "employee_id" (primary key), "name", "salary", and "dept_id". The "departments"
table has columns "dept_id" (primary key) and "name". Write an SQL query to retrieve the
names and salaries of all employees who work in a department whose name starts with "S".
2. Suppose you have three tables called "students", "courses", and "enrollments". The "students"
table has columns "student_id" (primary key), "name", and "email". The "courses" table has
columns "course_id" (primary key), "name", and "credits". The "enrollments" table has columns
"enrollment_id" (primary key), "student_id", "course_id", and "grade". Write an SQL query to
retrieve the names and emails of all students who have taken at least one course with a credit
value of 4.
3. Suppose you have two tables called "employees" and "managers". The "employees" table has
columns "employee_id" (primary key), "name", "salary", and "manager_id". The "managers"
table has columns "manager_id" (primary key), "name", and "department". Write an SQL query
to retrieve the names and salaries of all employees who work for a manager who manages the
"Sales" department.
4. Suppose you have two tables called "customers" and "orders". The "customers" table has
columns "customer_id" (primary key), "name", and "email". The "orders" table has columns
"order_id" (primary key), "customer_id", "order_date", and "total_price". Write an SQL query to
retrieve the names and email addresses of all customers who have made more than 5 orders.
5. Suppose you have two tables called "customers" and "orders". The "customers" table has
columns "customer_id" (primary key), "name", and "email". The "orders" table has columns
"order_id" (primary key), "customer_id", "order_date", and "total_price". Write an SQL query to
retrieve the total number of orders made by each customer.

Sure, here are the solutions to the SQL query questions I provided:

Question
SQL Query
Number

SELECT e.name, e.salary FROM employees e JOIN departments d ON e.dept_id =


1
d.dept_id WHERE d.name LIKE 'S%';

SELECT s.name, s.email FROM students s JOIN enrollments e ON s.student_id =


2
e.student_id JOIN courses c ON e.course_id = c.course_id WHERE c.credits = 4;

SELECT e.name, e.salary FROM employees e JOIN managers m ON e.manager_id =


3
m.manager_id WHERE m.department = 'Sales';

SELECT c.name, c.email FROM customers c WHERE c.customer_id IN (SELECT


4 o.customer_id FROM orders o GROUP BY o.customer_id HAVING COUNT(o.order_id)
> 5);

SELECT c.name, COUNT(o.order_id) as num_orders FROM customers c LEFT JOIN


5
orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id;

Now, let's go through each solution and explain how it works:

1. This query retrieves the names and salaries of all employees who work in a department whose
name starts with "S". We first join the "employees" and "departments" tables using the
dept_id column, and then use the WHERE clause to filter for department names that start with
"S". The JOIN clause ensures that we only retrieve information for employees who are in the
selected department.
2. This query retrieves the names and emails of all students who have taken at least one course
with a credit value of 4. We join the "students", "enrollments", and "courses" tables using their
respective ids, and then use the WHERE clause to filter for courses with a credit value of 4. The
JOIN clause ensures that we only retrieve information for students who have enrolled in at
least one of these courses.
3. This query retrieves the names and salaries of all employees who work for a manager who
manages the "Sales" department. We join the "employees" and "managers" tables using the
manager_id column, and then use the WHERE clause to filter for managers who manage the
"Sales" department. The JOIN clause ensures that we only retrieve information for employees
who are under one of these managers.
4. This query retrieves the names and email addresses of all customers who have made more
than 5 orders. We use a subquery to first group the "orders" table by customer_id and count
the number of orders each customer has made. Then, we use the IN keyword to retrieve
information only for customers whose id is in the resulting set from the subquery.
5. This query retrieves the total number of orders made by each customer. We use a LEFT JOIN
to join the "customers" and "orders" tables using the customer_id column, and then use the
COUNT keyword to count the number of orders made by each customer. The GROUP BY
clause ensures that we get one row per customer. Note that we use a LEFT JOIN instead of an
INNER JOIN to also include customers who have not made any orders in the result set.

Group By & Count Query

The GROUP BY query in SQL is used to group rows in a table based on one or more columns. It
allows us to perform aggregate functions on the grouped data, such as counting, summing,
averaging, etc. The GROUP BY clause is typically used in conjunction with aggregate functions to
provide summarized information.

Here's the general syntax of a GROUP BY query:

SELECT column1, column2, aggregate_function(column) language-sql


FROM table
GROUP BY column1, column2

Let's consider an example to better understand the GROUP BY query:

Suppose we have a table called "sales" with columns "product", "category", and "quantity_sold".
We want to find the total quantity sold for each category. We can use the GROUP BY query as
follows:

SELECT category, SUM(quantity_sold) AS total_sold language-sql


FROM sales
GROUP BY category;

This query will group the rows by the "category" column and calculate the sum of "quantity_sold"
for each category. The result will be a table with two columns: "category" and "total_sold", showing
the total quantity sold for each category.

The HAVING clause is used in conjunction with the GROUP BY clause to filter the grouped data
based on specified conditions. It allows us to apply conditions to the result of the GROUP BY
query.

Let's extend the previous example to include the HAVING clause:

SELECT category, SUM(quantity_sold) AS total_sold language-sql


FROM sales
GROUP BY category
HAVING total_sold > 100;

In this query, we have added the HAVING clause to filter the result based on the condition
total_sold > 100 . It will return only those categories that have a total quantity sold greater than
100.
Another example could be finding the average price of products in each category but only
including categories with an average price greater than 50:

SELECT category, AVG(price) AS avg_price language-sql


FROM products
GROUP BY category
HAVING avg_price > 50;

In this query, we calculate the average price for each category using the AVG() function and then
filter the result using the HAVING clause to include only those categories with an average price
greater than 50.

In summary, the GROUP BY query is used to group rows based on one or more columns, and the
HAVING clause is used to filter the grouped data based on specified conditions. Together, they
allow us to perform aggregations and apply conditions to the grouped data.

The COUNT function in SQL is used to count the number of rows that match a specified condition
or the total number of rows in a table. It is an aggregate function that can be applied to a single
column or used in combination with other columns and functions.

Here are a few examples to illustrate the use of COUNT:

1. Counting all rows in a table:

SELECT COUNT(*) AS total_rows language-sql


FROM table_name;

This query will return the total number of rows in the table.
2. Counting rows that meet a specific condition:

SELECT COUNT(*) AS total_employees language-sql


FROM employees
WHERE salary > 50000;

This query will count the number of employees whose salary is greater than 50,000.
3. Counting distinct values in a column:

SELECT COUNT(DISTINCT category) AS unique_categories language-sql


FROM products;

This query will count the number of unique categories in the "category" column of the
"products" table.
4. Counting rows in a group using GROUP BY:

SELECT department, COUNT(*) AS employees_count language-sql


FROM employees
GROUP BY department;

This query will group the employees by department and count the number of employees in
each department.
5. Combining COUNT with other aggregate functions:

language-sql
SELECT department, COUNT(*) AS employees_count, AVG(salary) AS average_salary
FROM employees
GROUP BY department;

This query will group the employees by department and calculate both the count of
employees and the average salary for each department.

The COUNT function is particularly useful for generating summary information, performing data
analysis, and filtering data based on row counts. It provides valuable insights into the size and
distribution of data within tables.

You might also like