SQL Query Practice
SQL Query Practice
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.
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.
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
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.
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.
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:
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.
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:
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.
This query will return the total number of rows in the table.
2. Counting rows that meet a specific condition:
This query will count the number of employees whose salary is greater than 50,000.
3. Counting distinct values in a column:
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:
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.