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

Database Assignment

The document describes sample tables for customers and orders data. It provides 20 queries to retrieve different types of information from the tables, such as customer names and order details, total amounts spent by customers, and customers who placed orders on specific dates.

Uploaded by

p p
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Database Assignment

The document describes sample tables for customers and orders data. It provides 20 queries to retrieve different types of information from the tables, such as customer names and order details, total amounts spent by customers, and customers who placed orders on specific dates.

Uploaded by

p p
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Old Assignment

Customers Table:

customer_id first_name last_name email


1 John Smith [email protected]
2 Jane Doe [email protected]
3 David Johnson [email protected]
Orders Table:

order_id customer_id order_date total_amount


1 1 2023-01-15 50.00
2 2 2023-02-02 75.00
3 3 2023-03-10 100.00
4 1 2023-03-25 30.00
5 2 2023-04-05 45.00

1. Retrieve all records from the "Customers" table.


2. Retrieve the names of all customers from the "Customers" table.
3. Retrieve the names and emails of customers whose last name is "Smith" from the
"Customers" table.
4. Retrieve the names, order IDs, and order dates of customers who have placed orders from
the "Customers" and "Orders" tables.
5. Retrieve the total number of customers in the "Customers" table.
6. Retrieve the average price of all products in the "Products" table.
7. Retrieve the names and quantities of products that have a quantity greater than 50 in the
"Products" table.
8. Retrieve the names and prices of products that have a price between $10 and $20 in the
"Products" table.

Retrieve the customers who have spent more than $50 in a single order.

1. Retrieve the customers who have not placed any orders.


2. Retrieve the customers who have placed orders in February 2023.
3. Retrieve the total amount spent by each customer.
4. Retrieve the customer(s) who have placed the highest total amount of orders.
5. Retrieve the customer(s) who have placed the lowest total amount of orders.
6. Retrieve the customer(s) who have the highest total amount spent in a single order.
7. Retrieve the customer(s) who have the lowest total amount spent in a single order.
8. Retrieve the customer(s) who have placed orders both in January and March 2023.
9. Retrieve the customers whose email addresses end with "@example.com".
10. Retrieve the customer(s) who have placed the most recent order.
11. Retrieve the customer(s) who have not placed any orders in the year 2023.
12. Retrieve the customers who have spent more than the average amount in a single
order.
13. Retrieve the customer(s) who have placed orders on the same day they registered as
customers.
14. Retrieve the customer(s) who have placed orders on weekends (Saturday or Sunday).
New Assignment
Table: Customers
+----+-------------+-----------+
| ID | Name | City |
+----+-------------+-----------+
| 1 | John Doe | New York |
| 2 | Jane Smith | London |
| 3 | Mike Johnson| Paris |
+----+-------------+-----------+

Table: Orders
+----+---------+------------+
| ID | Item | Customer |
+----+---------+------------+
| 1 | Laptop | 2 |
| 2 | Mouse | 1 |
| 3 | Keyboard| 3 |
+----+---------+------------+

Write SQL queries to solve the following exercises:

1. Retrieve all customers from the "Customers" table.


2. Retrieve the names of customers who live in "London".
3. Retrieve the items and their corresponding customers from the "Orders" table.
4. Retrieve the total count of orders placed.
5. Retrieve the names of customers and the count of orders they have placed.
6. Retrieve the names of customers who live in "Paris".
1. Retrieve the details of the order with ID 2.
2. Retrieve the names of customers who have placed an order.
3. Retrieve the names of customers who have not placed any orders.
4. Retrieve the count of customers for each city.
5. Retrieve the average count of orders placed by customers.
6. Retrieve the customer IDs and their corresponding order counts.
7. Retrieve the customers and their orders in descending order of the order ID.
8. Retrieve the customers and their orders in ascending order of the customer name.
9. Retrieve the customers and their orders sorted by the city in descending order and the order
ID in ascending order.
10. Retrieve the customers who have placed more than 1 order.
11. Retrieve the customers who have placed an order for a "Laptop".
12. Retrieve the customer names and the total number of characters in their names.
13. Retrieve the customers whose names start with the letter "J".
14. Retrieve the customers whose names end with the letter "n".
15. Retrieve the customers whose names contain the letter "a".
16. Retrieve the customers whose names have exactly 8 characters.
17. Retrieve the customers whose names have at least 5 characters and at most 10
characters.
18. Retrieve the customers whose names are not null.
19. Retrieve the customers whose names are alphabetically between "Jane Smith" and
"Mike Johnson".
20. Retrieve the customers who have placed an order for a "Keyboard".
1. Retrieve the customers who have placed an order for an item that starts with the letter "M".
2. Retrieve the customers who have placed an order for an item that contains the letter "o".
3. Retrieve the customers who have placed an order for an item that ends with the letter "e".
4. Retrieve the customers who have placed an order for an item with exactly 6 characters.
5. Retrieve the customers who have placed an order for an item with at least 5 characters and at
most 8 characters.
6. Retrieve the customers who have placed an order for an item that is either "Laptop" or
"Mouse".
7. Retrieve the customers who have placed an order for an item that is not "Keyboard".
8. Retrieve the customers who live in "New York" and have placed an order.
9. Retrieve the customers who live in cities other than "Paris" and have placed an order.
10. Retrieve the customers who live in cities that start with the letter "L".
11. Retrieve the customers who live in cities that end with the letter "n".
12. Retrieve the customers who live in cities that contain the letter "a".
13. Retrieve the customers who live in cities with exactly 6 characters.
14. Retrieve the customers who live in cities with at least 5 characters and at most 8
characters.
15. Retrieve the customers who live in cities that are not "London".
16. Retrieve the customers who live in cities alphabetically between "London" and
"Paris".
17. Retrieve the customers who live in cities alphabetically before "London".
18. Retrieve the customers who live in cities alphabetically after "Paris".
19. Retrieve the customers who live in cities that have more than one word in their name.

2)Consider the following table diagram:


Sql code
Table: Employees
+----+------------+----------+--------+
| ID | First_Name | Last_Name| Salary |
+----+------------+----------+--------+
| 1 | John | Doe | 5000 |
| 2 | Jane | Smith | 6000 |
| 3 | Mike | Johnson | 4500 |
+----+------------+----------+--------+

Table: Departments
+----+------------+
| ID | Department |
+----+------------+
| 1 | Sales |
| 2 | IT |
| 3 | Finance |
+----+------------+

Write SQL queries to solve the following exercises:


1. Retrieve the names of employees and their corresponding department.
2. Retrieve the names of employees and their corresponding department in alphabetical order
of the department name.
3. Retrieve the names of employees whose salary is greater than 5000.
4. Retrieve the names of employees whose salary is between 4000 and 6000 (inclusive).
5. Retrieve the names of employees who are in the "Sales" department.
6. Retrieve the names of employees who are not in the "IT" department.
7. Retrieve the average salary of all employees.
8. Retrieve the maximum salary among all employees.
9. Retrieve the total number of employees.
10. Retrieve the names of employees and their salaries in descending order of the salary.
11. Retrieve the names of employees and their salaries in ascending order of the salary,
and then by the employee's last name in alphabetical order.
12. Retrieve the names of employees and their corresponding department, sorted by the
employee's last name in descending order.
13. Retrieve the names of employees whose first name starts with the letter "J" and their
salary is greater than 4000.
14. Retrieve the names of employees whose last name ends with the letter "n" or "h".
15. Retrieve the names of employees whose first name contains the letter "a" and last
name contains the letter "o".
16. Retrieve the names of employees whose first name has exactly 4 characters and last
name has exactly 3 characters.
17. Retrieve the names of employees whose first name has at least 3 characters and last
name has at most 5 characters.
18. Retrieve the names of employees whose salary is higher than the average salary.
19. Retrieve the names of employees whose salary is higher than the maximum salary in
the "Sales" department.
20. Retrieve the names of employees who have the same first name.

You might also like