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

sql keys

The document contains a series of SQL queries for various operations, including selecting distinct numbers, joining tables, calculating averages, deleting duplicates, and creating procedures. It also includes commands for data manipulation, such as updating records and altering table structures. Each SQL statement is designed to perform specific tasks related to data retrieval and management in a database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

sql keys

The document contains a series of SQL queries for various operations, including selecting distinct numbers, joining tables, calculating averages, deleting duplicates, and creating procedures. It also includes commands for data manipulation, such as updating records and altering table structures. Each SQL statement is designed to perform specific tasks related to data retrieval and management in a database.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

sql94

SELECT DISTINCT num as ConsecutiveNums


FROM (SELECT
num,
LAG(num, 1) OVER (ORDER BY id) as prev_num,
-- This expression retrieves the value of the num column
from the previous row, based on the id order.

LAG(num, 2) OVER (ORDER BY id) as prev2_num,


-- This expression retrieves the value of the num column
2 rows back, based on the id order.

LEAD(num, 1) OVER (ORDER BY id) as next_num,


---- This expression retrieves the value of the num column
of the next row, based on the id order.

LEAD(num, 2) OVER (ORDER BY id) as next2_num


FROM Logs) as lg
WHERE
(num = prev_num AND prev_num = prev2_num)
OR (prev_num = num AND num = next_num)
OR (num = next_num AND next_num = next2_num)
---
Sql96
SELECT ISNULL(u.unique_id, NULL) AS unique_id, e.name
FROM Employees as e LEFT JOIN EmployeeUNI as u
ON e.id=u.id
---
sql97
SELECT
a1.machine_id,
ROUND(AVG(a2.timestamp - a1.timestamp), 3) AS
processing_time
FROM
Activity a1
JOIN Activity a2 ON
a1.machine_id = a2.machine_id
AND a1.process_id = a2.process_id
AND a1.activity_type = 'start'
AND a2.activity_type = 'end'
GROUP BY
a1.machine_id;
---
SQL98
DELETE p1
FROM Person p1
JOIN Person p2 ON p1.email = p2.email
WHERE p1.id > p2.id
---
Sql99
select name from Customer
where referee_id != '2' or referee_id is null
---
SQL100 - Recycle and low fat product
select product_id
from Products
where low_fats='Y' and recyclable = 'Y'
---
SQL101 - Not boring movie
select * from Cinema
where description != 'boring' and id %2=1
order by rating desc
---
SQL102 - Rising temperature
SELECT w1.Id
FROM Weather w1
JOIN Weather w2 ON DATEDIFF(day, w2.recordDate,
w1.recordDate) = 1
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, c.dept, c.number
FROM Instructor i
JOIN Class c
ORDER BY i.username DESC
LIMIT 2;
---
SQL111 - Sửa tiêu đề gõ sai
select * from Class
where lower(title) like 'introduction%'
---
SQL114 - Những instructors nào bắt đầu dạy trước 1990
select username, fname, lname, started_on
from Instructor
where year(started_on)<1990
group by username, fname, lname, started_on
order by started_on asc
---
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('2024-01-01', INTERVAL 2
YEAR)
group by username, fname, lname, started_on
order by started_on desc
---
SQL117 - Sửa dữ liệu sai trong bảng
ALTER TABLE party_guests
MODIFY COLUMN age INT,
MODIFY COLUMN drinks_count INT;
---
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)
);
---
SQL120 - Tạo procedure lấy danh sách nhân viên
CREATE PROCEDURE GetEmployeeById
@employeeId INT
AS
BEGIN
SELECT * FROM Employees WHERE id = @employeeId
END
EXEC GetEmployeeById 10
---
SQL125 - Cập nhật kết quả sinh viên
UPDATE SinhVien
SET TrangThai = CASE
WHEN DiemTB >= 5.0 THEN 'Đạt'
ELSE 'Không đạt'
END;
---
SQL126 - Confirmation Rate
SELECT s.user_id, COUNT(CASE WHEN c.action =
'confirmed' THEN 1 END) / COUNT(*) AS
confirmation_rate
FROM Signups s left JOIN Confirmations c ON s.user_id =
c.user_id
GROUP BY s.user_id;
---
SQL127 - Number of Unique Subject taught by each
teacher
SELECT teacher_id, COUNT(DISTINCT subject_id) as cnt
FROM Teacher
GROUP BY teacher_id
---
SQL130 - Product Sales Anaysis
select product_name, year, price
from Sales s join Product p
where s.product_id=p.product_id
---
SQL131 - Customer who bought all product
select c.customer_id
from (select customer_id, count(product_key) as cnt
from Customer group by customer_id) as c
where c.cnt= (select count(product_key) from Product)
---
SQL134 - Movie rating
(SELECT name AS results
FROM MovieRating JOIN Users USING(user_id)
GROUP BY name
ORDER BY COUNT(*) DESC, name
LIMIT 1)
UNION ALL
(SELECT title AS results
FROM MovieRating JOIN Movies USING(movie_id)
WHERE EXTRACT(YEAR_MONTH FROM created_at) =
202002
GROUP BY title
ORDER BY AVG(rating) DESC, title
LIMIT 1);
---

You might also like