0% found this document useful (0 votes)
2 views5 pages

DBMS__Lab3_SQL_exercises

Lab 3 focuses on advanced SQL exercises using the classicmodels sample database to enhance students' SQL skills. The lab includes various exercises covering SQL queries such as SELECT, JOIN, GROUP BY, and INSERT statements. Students are required to submit their SQL queries in a specified format by a set deadline.

Uploaded by

btngan1011
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)
2 views5 pages

DBMS__Lab3_SQL_exercises

Lab 3 focuses on advanced SQL exercises using the classicmodels sample database to enhance students' SQL skills. The lab includes various exercises covering SQL queries such as SELECT, JOIN, GROUP BY, and INSERT statements. Students are required to submit their SQL queries in a specified format by a set deadline.

Uploaded by

btngan1011
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/ 5

Lab 3: Advanced SQL Exercises

Dr Hung Tran and Dr Minh Tran Duc

Faculty of Data Science and Artificial Intelligence


College of Technology
National Economics University, Vietnam
Email: [email protected]
Mobile: 086-646-4048

1
Faculty of Data Science and Artificial Intelligence, NEU

1 Objective
The objective of this lab is to provide advanced hands-on practice with SQL queries using the classicmod-
els sample database. By the end of this lab, students will be proficient in using various SQL keywords
and concepts.

2 Foreword
Dear students,
Welcome to Lab 3: Advanced SQL Exercises! This lab is designed to help you practice and
reinforce your SQL skills using the classicmodels sample database. Make sure you have loaded the
classicmodels database into your MySQL server as instructed in the previous lab.
All the best,
Course Instructors

3 Introduction to SQL Exercises


In this lab, you will perform various SQL queries to retrieve, manipulate, and analyze data from the
classicmodels database. Follow the instructions for each exercise and write the corresponding SQL queries

MySQL Sample Database Schema


The MySQL sample database schema consists of the following tables:

@DATCOM Lab 2
Faculty of Data Science and Artificial Intelligence, NEU

Table Name Description


customers Stores customer’s data.
products Stores a list of scale model cars.
productlines Stores a list of product lines.
orders Stores sales orders placed by customers.
orderdetails Stores sales order line items for every sales order.
payments Stores payments made by customers based on their accounts.
employees Stores employee information and the organization structure such as who reports to whom.
offices Stores sales office data.

4 Loading the Sample Database


If you have not yet loaded the classicmodels sample database, follow these steps:
• Open the MySQL command line client and connect to the MySQL Server.
• Create a new database to hold the sample data:

CREATE DATABASE classicmodels;

• Select the newly created database:

USE classicmodels;

• Load the sample data using the following command:

SOURCE /path/to/mysqlsampledatabase.sql;

Replace /path/to/mysqlsampledatabase.sql with the actual path to the provided SQL file.
• Verify the data has been loaded by listing the tables:

SHOW TABLES;

5 Exercises
5.1 Exercise 1: Basic Select and From
Write a SQL query to retrieve all records from the customers table. Display the customer number,
name, and country.

5.2 Exercise 2: Order By and Where


Write a SQL query to list the last name, first name, and office code of employees who work in the office
with office code 1, ordered by last name.

5.3 Exercise 3: Select Distinct


Write a SQL query to find all unique customer countries from the customers table.

5.4 Exercise 4: AND, OR, IN, NOT IN


Write a SQL query to find all customers who are located in the USA or Canada. Exclude customers
from California and Texas.

@DATCOM Lab 3
Faculty of Data Science and Artificial Intelligence, NEU

5.5 Exercise 5: BETWEEN and LIKE


Write a SQL query to find all orders placed between ’2003-01-01’ and ’2003-12-31’. Include orders where
the customer’s last name starts with ’A’.

5.6 Exercise 6: LIMIT and IS NULL


Write a SQL query to retrieve the first 10 products from the products table where the product vendor
is not specified (is NULL).

5.7 Exercise 7: Table and Column Aliases


Write a SQL query to list the product name as ’Product’, and the product line as ’Line’, from the
products table.

5.8 Exercise 8: Joins


5.8.1 INNER JOIN
Write a SQL query to list all orders with their corresponding customer names using an INNER JOIN.

5.8.2 LEFT JOIN


Write a SQL query to list all customers and their orders, including customers who have not placed any
orders, using a LEFT JOIN.

5.8.3 RIGHT JOIN


Write a SQL query to list all orders and their corresponding customer names, including orders without
a customer match, using a RIGHT JOIN.

5.8.4 Self Join


Write a SQL query to list all employees and their managers. Use a self join on the employees table.

5.8.5 CROSS JOIN


Write a SQL query to list all combinations of customers and products using a CROSS JOIN.

5.9 Exercise 9: GROUP BY and HAVING


Write a SQL query to calculate the total payments made by each customer. Only include customers who
have made more than 5 payments.

5.9.1 HAVING COUNT


Write a SQL query to list all product lines and the number of products in each line. Only include product
lines with more than 10 products.

5.9.2 ROLLUP
Write a SQL query to calculate the total sales per year, with a grand total at the end, using the ROLLUP
function.

5.10 Exercise 10: Subqueries and Derived Tables


5.10.1 Subquery
Write a SQL query to find all customers who have placed an order with a total amount greater than
50000.

@DATCOM Lab 4
Faculty of Data Science and Artificial Intelligence, NEU

5.10.2 Derived Tables


Write a SQL query to list the top 5 customers by total payments using a derived table.

5.10.3 EXISTS
Write a SQL query to find all customers who have placed at least one order.

5.11 Exercise 11: Insert Statements


5.11.1 Insert Into
Write a SQL query to insert a new customer into the customers table.

5.11.2 Insert Multiple Rows


Write a SQL query to insert three new products into the products table in a single query.

5.11.3 INSERT INTO SELECT


Write a SQL query to insert all customers from the USA into a new table called us customers.

5.11.4 Insert On Duplicate Key Update


Write a SQL query to insert a new order. If an order with the same order number already exists, update
its status.

5.11.5 INSERT IGNORE


Write a SQL query to insert a new office. If an office with the same office code already exists, do nothing.

5.11.6 Insert DateTimes and Dates


Write a SQL query to insert a new order with the current date and time as the order date.

6 Submission Instructions
• Write your SQL queries for each exercise and save them in a single SQL file named ID Lab3 Exercises.sql
(replace ID with your student ID).
• Ensure all queries are well-formatted and commented for clarity.

• Submit the SQL file into Team.


• Deadline: Ensure all submissions are made before the end of the second Friday (11:59 PM). Late
submissions without an approved extension will incur a penalty of 1 point per business day past
the deadline.

7 References
• MySQL Sample Database

@DATCOM Lab 5

You might also like