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

THCSDL

The document contains a series of SQL queries for the 'classicmodels' database, organized into practical exercises. These queries cover various operations such as selecting, updating, and deleting data from tables like employees, orders, products, and customers. The exercises also include joins, aggregations, and subqueries to manipulate and retrieve data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

THCSDL

The document contains a series of SQL queries for the 'classicmodels' database, organized into practical exercises. These queries cover various operations such as selecting, updating, and deleting data from tables like employees, orders, products, and customers. The exercises also include joins, aggregations, and subqueries to manipulate and retrieve data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

use classicmodels;

/*Thực hành số 3*/


/*1*/ select * from employees where reportsTo is null;
/*2*/ select distinct customerNumber from orders;
/*3*/ select * from orders where requiredDate = "2003-01-18";
/*4*/ select * from orders where status = "Shipped" and year(orderDate) = 2005 and
month(orderDate) = 4;
/*5*/ select * from products where productLine = "Classic Cars";

/*Thực hành số 4*/


/*1*/ select * from customers where city in ("Nantes", "Lyon");
select * from customers where city = "Nantes" or city = "Lyon";
/*2*/ select * from orders where shippedDate between "2003-01-10" and "2003-03-10";
select * from orders where shippedDate >= "2003-01-10" and shippedDate <= "2003-03-
10";
/*3*/ select distinct * from products where productLine like "%Cars%";
/*4*/ select productCode, productName, quantityInStock from products order by
quantityInStock desc limit 10;
/*5*/ select productCode, productName, quantityInStock, buyPrice,
quantityInStock*buyPrice as priceOfInStock from products;

/*Thực hành số 5*/


/*1*/ select substring(productDescription, 1, 50) as 'Title of products' from
products;
/*2*/ select concat_ws(',', concat_ws(' ', firstName, lastName), concat(jobTitle,
'.')) as employeeDescription from employees;
/*3*/ SET SQL_SAFE_UPDATES = 0;
SET foreign_key_checks = 0;
update productlines set productLine = replace(productLine, 'Cars', 'Automobiles');
update products set productLine = replace (productLine, 'Cars', 'Automobiles');
SET SQL_SAFE_UPDATES = 1;
SET foreign_key_checks = 1;
/*4*/ select *, datediff(requiredDate, shippedDate) as dateDiff from orders order
by dateDiff desc limit 5;
/*5*/ select orderNumber, orderDate, shippedDate from orders where month(orderDate)
= 5 and year(orderDate) = 2005 and shippedDate is null;

/*Thực hành số 6*/


/*1*/ select city, count(*) as numOfCustomers from customers group by city;
/*2*/ select month(orderDate) as month, count(*) as quantity from orders where
year(orderDate)=2005 and month(orderDate)=3 group by month(orderDate);
/*2*/ select month(orderDate) as month, count(*) as quantity from orders where
year(orderDate)=2005 group by month(orderDate);
/*3*/ select orderNumber, sum(quantityOrdered * priceEach) as totalValue from
orderdetails group by orderNumber order by totalValue desc limit 10;
/*4*/ select productLine, sum(quantityInStock) as quantity from products group by
productLine;
/*5*/ select customerNumber, sum(amount) as totalConsumed from payments group by
customerNumber;

/*Thực hành số 7*/


/*1*/ select o.city as officeName, e.* from employees e join offices o on
e.officeCode = o.officeCode;
/*2*/ select p.*, o.orderNumber from products p left join orderdetails o on
p.productCode = o.productCode where o.orderNumber is null;
/*3*/ select o.orderDate, o.requiredDate, o.status, (od.quantityOrdered *
od.priceEach) as totalValue from orders o join orderdetails od on o.orderNumber =
od.orderNumber where month(o.orderDate) = 3 and year(o.orderDate) = 2003;
/*4*/ select p.productLine, sum(p.quantityInStock) as totalProduct,
pl.textDescription from products p join productlines pl on p.productLine =
pl.productLine group by p.productLine order by sum(p.quantityInStock) desc;
/*5*/ select c.customerName, sum(od.priceEach * od.quantityOrdered) as
totalConsumption from customers c
join orders o on c.customerNumber = o.customerNumber
join orderdetails od on o.orderNumber = od.orderNumber
group by c.customerNumber;

/*Thực hành số 8*/


/*1*/ select * from products where productCode in
(select productCode from orderdetails where orderNumber in
(select orderNumber from orders where month(orderDate) = 3 and
year(orderDate) = 2005));
/*2*/ select * from orders o where
month(o.orderDate) = (select month(max(orderDate)) from orders)
and year(o.orderDate) = (select year(max(orderDate)) from orders);

/*3*/ select * from


orders o,
(select orderNumber, productCode, sum(priceEach*quantityOrdered) as
totalPrice
from orderdetails group by orderNumber) as od
where o.orderNumber = od.orderNumber;
/*4*/
select cus_price.customerNumber, cus_price.customerName, (totalPrice - sum(amount))
as inDebt from
payments p,
(select c.customerName, c.customerNumber, sum(sumPrice) as totalPrice from
customers c,
(select o.customerNumber, sumPrice from
orders o,
(select orderNumber, sum(priceEach*quantityOrdered) as sumPrice
from orderdetails group by orderNumber) as od
where o.orderNumber = od.orderNumber) as cus_od
where c.customerNumber = cus_od.customerNumber
group by cus_od.customerNumber) as cus_price
where cus_price.customerNumber = p.customerNumber
group by cus_price.customerNumber;

/*Thực hành số 9*/


/*1*/
insert into orders (orderNumber, orderDate, requiredDate, status, customerNumber)
values
(10427, '2021-04-20', '2021-04-29', 'In Process', 103),
(10428,'2021-04-21', '2021-05-02', 'In Process',
(select customerNumber from customers where contactLastName = 'King'
and contactFirstName = 'Jean'));
insert into productlines (productLine, textDescription)
values
('Bicycle', 'New Bicycle product line for children under 5');
update customers
set phone = '+840126548753'
where customerNumber = 103;

set foreign_key_checks = 0;
delete products, productlines from products, productlines
where products.productLine = productlines.productLine and
productlines.productLine = 'Ships';
set foreign_key_checks = 1;
/*2*/
create table temp_orderdetails like orderdetails;

insert into orderdetails (orderNumber, productCode, quantityOrdered, priceEach,


orderLineNumber)
values
(10428, 'S10_2016', 1, 68.99, 10);
insert into temp_orderdetails
select * from orderdetails where orderdetails.orderNumber in
(select orderNumber from orders where orders.orderDate = (select
max(orderDate) from orders));
/*3*/
set sql_safe_updates = 0;
update employees
set jobTitle = 'Sales Representatives'
where jobTitle = 'Sales Rep';
set sql_safe_updates = 1;

/*GK1*/
/*Cau 1*/
show databases;

use classicmodels;
show tables;

/*Cau 2*/
select distinct productCode from orderdetails;

select orderNumber, datediff(shippedDate, orderDate) as dateForward from orders


where shippedDate is not null order by dateForward asc limit 5;

/*Cau 3*/
select concat_ws(' ', addressLine1, if (addressLine2 is null, 'N/A', addressLine2))
address from customers;

/*Cau 4*/
alter table partner add constraint fk_partner_employees foreign key
(employeeNumber) references employees (employeeNumber) on delete restrict on update
cascade;

You might also like