0% found this document useful (0 votes)
23 views7 pages

Tong Hop

The document contains a series of SQL queries for various database operations including selecting, updating, and deleting records from different tables. It covers employee bonuses, supplier lists, product details, customer orders, and more. Each query is designed to retrieve specific information or perform actions on the data stored in the database.

Uploaded by

huytam514z
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)
23 views7 pages

Tong Hop

The document contains a series of SQL queries for various database operations including selecting, updating, and deleting records from different tables. It covers employee bonuses, supplier lists, product details, customer orders, and more. Each query is designed to retrieve specific information or perform actions on the data stored in the database.

Uploaded by

huytam514z
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/ 7

SQL139 - Empoyee bonus

select e.name,b.Bonus
from Employee e
left join Bonus b on e.empId = b.empId
where b.Bonus < 1000 or b.Bonus IS NULL

SQL147 - Danh sách các đối tác cung cấp hàng cho công ty

SELECT *
FROM NHACUNGCAP

SQL148 - Mã hàng, tên hàng và số lượng của các mặt hàng hiện có trong công ty

SELECT MaHang, TenHang, SoLuong


FROM MATHANG

SQL149 - Họ tên, địa chỉ và năm bắt đầu làm việc của các nhân viên trong cty

select ho,ten,diachi,year(ngaylamviec)
from nhanvien

SQL150 - Cho biết mỗi mặt hàng trong công ty do ai cung cấp

select mh.tenhang,mh.macongty,ncc.tencongty
from mathang mh
join nhacungcap ncc on ncc.macongty = mh.macongty
order by mh.tenhang ASC

SQL151 - Loại hàng "furniture" do những công ty nào cung cấp, địa chỉ của công ty
đó

select lh.tenloaihang,ncc.tencongty,ncc.diachi
from loaihang lh
join mathang mh on mh.maloaihang = lh.maloaihang
join nhacungcap ncc on ncc.macongty = mh.macongty
where lh.tenloaihang = "furniture"

SQL152 - Những khách hàng nào (tên giao dịch) đã đặt mua mặt hàng "chair" của công
ty

select kh.tengiaodich,mh.tenhang
from khachhang kh
join dondathang ddh on ddh.makhachhang = kh.makhachhang
join chitietdathang ctdh on ctdh.sohoadon = ddh.sohoadon
join mathang mh on mh.mahang = ctdh.mahang
where mh.tenhang like '%chair'

SQL153 - Đơn đặt hàng số 1001 do ai đặt và do nhân viên nào lập, thời gian và địa
điểm giao hàng là ở đâu

select ddh.sohoadon,kh.tengiaodich,nv.ten as tennhanvien,ddh.ngaydathang,


ddh.ngaygiaohang,ddh.noigiaohang
from dondathang ddh
join nhanvien nv on nv.manhanvien = ddh.manhanvien
join khachhang kh on kh.makhachhang = ddh.makhachhang
where ddh.sohoadon = '1001'

SQL155 - Hãy cho biết số tiền lương mà công ty phải trả cho mỗi nhân viên là bao
nhiêu

select manhanvien, (luongcoban + phucap) as luong


from nhanvien

SQL156 - Số tiền mà khách hàng phải trả cho mỗi mặt hàng

select ddh.sohoadon,mh.tenhang,ctdh.giaban,ctdh.soluong,ctdh.mucgiamgia,
(ctdh.soluong * ctdh.giaban) * (1 - ctdh.mucgiamgia/100) as sotienphaitra
from dondathang ddh
join chitietdathang ctdh on ctdh.sohoadon = ddh.sohoadon
join mathang mh on mh.mahang = ctdh.mahang
where ddh.sohoadon = '1002'

SQL138 - Fix name in a table

UPDATE Users
SET name = CONCAT(UPPER(LEFT(name, 1)), LOWER(SUBSTRING(name, 2)))

SQL137 - Managers with at least 5 direction report

SELECT e1.name AS name


FROM Employee e1
JOIN Employee e2
ON e1.id = e2.managerId
GROUP BY e1.id, e1.name
HAVING COUNT(e2.id) >= 5

SQL136 - Students and examinations

SELECT s.student_id,
s.student_name,
sub.subject_name,
COALESCE(COUNT(e.student_id), 0) AS attended_exams
FROM Students s
CROSS JOIN Subjects sub
LEFT JOIN Examinations e
ON s.student_id = e.student_id AND sub.subject_name = e.subject_name
GROUP BY s.student_id, s.student_name, sub.subject_name
ORDER BY s.student_id, sub.subject_name

SQL132 - Làm quen với LearnSQL


=>
select * from learnsql
SQL94 - Consecutive number
SELECT DISTINCT l1.num AS ConsecutiveNums
FROM Logs l1
JOIN Logs l2 ON l1.id = l2.id - 1
JOIN Logs l3 ON l1.id = l3.id - 2
WHERE l1.num = l2.num AND l2.num = l3.num;
SQL95 - Big Countries
SELECT name, population, area
FROM World
WHERE area >= 3000000 OR population >= 25000000;

SQL97 - Average time of process per machine


SELECT
machine_id,
ROUND(AVG(end_time - start_time), 3) AS processing_time
FROM (
SELECT
machine_id,
process_id,
MAX(CASE WHEN activity_type = 'end' THEN timestamp END) AS end_time,
MAX(CASE WHEN activity_type = 'start' THEN timestamp END) AS start_time
FROM Activity
GROUP BY machine_id, process_id
) process_times
GROUP BY machine_id;

SQL98 - Delete duplicate emails


DELETE FROM Person
WHERE id NOT IN (
SELECT MIN(id)
FROM Person
GROUP BY email
);

SQL99 - Find custom referee


SELECT name
FROM Customer
WHERE referee_id IS NULL OR referee_id != 2;
SQL100 - Recycle and low fat product
SELECT product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
SQL101 - Not boring movie
SELECT id, movie, description, rating
FROM Cinema
WHERE id % 2 != 0
AND description != 'boring'
ORDER BY rating DESC;

SQL102 - Rising temperature


SELECT w1.id
FROM Weather w1
JOIN Weather w2 ON DATE_ADD(w2.recordDate, INTERVAL 1 DAY) = w1.recordDate
WHERE w1.temperature > w2.temperature;

SQL103 - Liệt kê 2 khóa học theo tên giảng viên (sắp xếp theo tên giảm dần)
SELECT i.username, t.dept, t.number
FROM Teaches t
JOIN Instructor i ON t.username = i.username
JOIN Class c ON t.dept = c.dept AND t.number = c.number
ORDER BY i.lname DESC,i.fname DESC
limit 2
SQL106 - Tìm firstname của Instructor
SELECT fname
FROM Instructor
WHERE username = 'zahorjan';

SQL107 - Các khóa học cấp độ 400 (4xx) của CSE đang mở là gì
SELECT c.dept, c.number, c.title
FROM Class c
JOIN Teaches t ON c.dept = t.dept AND c.number = t.number
JOIN Instructor i ON t.username = i.username
WHERE c.dept = 'CSE' AND c.number BETWEEN 400 AND 499
GROUP BY c.dept, c.number, c.title;

SQL118 - Thiết kế cơ sở dữ liệu với bảng đăng ký khóa học


CREATE TABLE Enrollments (
enrollment_id INT PRIMARY KEY,
student_id INT,
course_id INT,
enrollment_date DATE,
FOREIGN KEY (student_id) REFERENCES Students(student_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);

SQL111 - Sửa tiêu đề gõ sai


SELECT
dept,
number,
title
FROM
Class
WHERE
LOWER(title) LIKE LOWER('%Introduction%');

SQL110 - Những khóa học nào có tên bắt đầu bằng "Introduction"
SELECT
dept,
number,
title
FROM
Class
WHERE
title LIKE 'Introduction%';

SQL108 - Tìm lớp học của thầy Levy


SELECT username, dept, number
FROM Teaches
WHERE username IN ('levy', 'djw');

SQL113 - Chuẩn hóa tên các khóa học


SELECT
dept,
number,
SUBSTRING(title, 1, 12) AS short_title
FROM
Class;

SQL114 - Những instructors nào bắt đầu dạy trước 1990


SELECT username, fname, lname, started_on
FROM Instructor
WHERE started_on < '1990-01-01'
ORDER BY started_on ASC;
SQL115 - Những instructors nào bắt đầu dạy trước thời điểm hiện tại
SELECT
username,
fname,
lname,
started_on
FROM
Instructor
WHERE
started_on <= CURDATE();

SQL116 - Những instructors bắt đầu dạy vào hoặc sau ngày 1 tháng 1 của 20 năm trước
SELECT
username,
fname,
lname,
started_on
FROM
Instructor
WHERE
started_on >= DATE_SUB(CURDATE(), INTERVAL 20 YEAR);

SQL117 - Sửa dữ liệu sai trong bảng


ALTER TABLE party_guests
MODIFY COLUMN age INT,
MODIFY COLUMN drinks_count INT;

SQL120 - Tạo procedure lấy danh sách nhân viên


CREATE PROCEDURE GetEmployeeById(IN employeeId INT)
BEGIN
SELECT id, name
FROM Employees
WHERE id = employeeId;
END;

SQL125 - Cập nhật kết quả sinh viên


UPDATE SinhVien
SET TrangThai = CASE
WHEN DiemTB >= 5.0 THEN 'Đạt'
WHEN DiemTB < 5.0 THEN 'Không đạt'
END;

SQL127 - Number of Unique Subject taught by each teacher


SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt
FROM Teacher
GROUP BY teacher_id;

SQL128 - Class more than 5 student


SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(student) >= 5;

SQL129 - Invalid Tweets


SELECT tweet_id
FROM Tweets
WHERE LENGTH(content) > 15;
SQL131 - Customer who bought all product
SELECT customer_id
FROM Customer c
GROUP BY customer_id
HAVING COUNT(DISTINCT c.product_key) = (SELECT COUNT(*) FROM Product);

SQL133 - Average Selling Price


SELECT p.product_id,
ROUND(SUM(u.units * p.price) / SUM(u.units), 2) AS average_price
FROM Prices p
LEFT JOIN UnitsSold u
ON p.product_id = u.product_id
AND u.purchase_date BETWEEN p.start_date AND p.end_date
GROUP BY p.product_id;

SQL134 - Movie rating


SELECT
(SELECT name
FROM Users u
JOIN (
SELECT user_id, COUNT(DISTINCT movie_id) AS num_movies
FROM MovieRating
GROUP BY user_id
) r ON u.user_id = r.user_id
ORDER BY r.num_movies DESC, u.name
LIMIT 1) AS results

UNION ALL

SELECT
(SELECT title
FROM Movies m
JOIN (
SELECT movie_id, AVG(rating) AS avg_rating
FROM MovieRating
WHERE created_at BETWEEN '2020-02-01' AND '2020-02-29'
GROUP BY movie_id
) r ON m.movie_id = r.movie_id
ORDER BY r.avg_rating DESC, m.title
LIMIT 1) AS results;

SQL136 - Students and examinations


SELECT
s.student_id,
s.student_name,
sub.subject_name,
COALESCE(COUNT(e.student_id), 0) AS attended_exams
FROM
Students s
CROSS JOIN
Subjects sub
LEFT JOIN
Examinations e ON s.student_id = e.student_id AND sub.subject_name =
e.subject_name
GROUP BY
s.student_id, s.student_name, sub.subject_name
ORDER BY
s.student_id, sub.subject_name;

You might also like