Bộ dữ liệu luyện tập
Sáu bộ dữ liệu quan hệ được dùng lại trong toàn bộ bài luyện và ví dụ bài giảng. Xem cấu trúc bảng, thử truy vấn mẫu rồi chuyển sang luyện tập.
6 bộ dữ liệu · 17 bảng
Bán lẻ
Khách hàng, sản phẩm, đơn hàng và chi tiết đơn — bộ dữ liệu chuẩn để luyện JOIN nhiều bảng, doanh thu theo ngành hàng và phân tích đơn hàng.
Cấu trúc bảng
customers
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| text | |
| city | text |
| signup_date | date |
| segment | text |
products
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| category | text |
| price | numeric(12, 2) |
| stock | integer |
orders
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| customer_id | integer → customers.id |
| order_date | date |
| status | text |
order_items
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| order_id | integer → orders.id |
| product_id | integer → products.id |
| quantity | integer |
| unit_price | numeric(12, 2) |
Truy vấn mẫu
select c.city, count(*) as so_don
from orders o
join customers c on c.id = o.customer_id
where o.status <> 'cancelled'
group by c.city
order by so_don desc;Thuê bao
Người đăng ký, gói dịch vụ và các lần thanh toán — dùng cho bài toán churn, doanh thu định kỳ và tỷ lệ gia hạn.
Cấu trúc bảng
subscribers
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| signup_date | date |
| country | text |
subscriptions
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| subscriber_id | integer → subscribers.id |
| plan | text |
| started_on | date |
| ended_on | date |
| monthly_price | numeric(12, 2) |
payments
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| subscription_id | integer → subscriptions.id |
| paid_on | date |
| amount | numeric(12, 2) |
| status | text |
Truy vấn mẫu
select plan, count(*) as so_nguoi_dang_dung
from subscriptions
where ended_on is null
group by plan
order by so_nguoi_dang_dung desc;Ngân hàng
Chi nhánh, tài khoản và giao dịch — phù hợp để luyện tổng hợp theo loại giao dịch, số dư và hoạt động theo chi nhánh.
Cấu trúc bảng
branches
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| city | text |
accounts
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| account_no | text |
| customer_name | text |
| branch_id | integer → branches.id |
| opened_on | date |
| balance | numeric(14, 2) |
transactions
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| account_id | integer → accounts.id |
| txn_date | date |
| type | text |
| amount | numeric(14, 2) |
Truy vấn mẫu
select type, sum(amount) as tong_tien
from transactions
group by type
order by tong_tien desc;Nhân sự
Phòng ban và nhân viên với mức lương, chức danh, ngày vào làm — luyện GROUP BY, AVG và so sánh lương giữa các phòng.
Cấu trúc bảng
departments
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
employees
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| department_id | integer → departments.id |
| title | text |
| salary | numeric(12, 2) |
| hire_date | date |
Truy vấn mẫu
select d.name as phong_ban, avg(e.salary) as luong_trung_binh
from employees e
join departments d on d.id = e.department_id
group by d.name
order by luong_trung_binh desc;Bệnh viện
Bệnh nhân và các lần khám với phí, khoa — luyện đếm lượt khám, tổng chi phí và phân tích theo nhóm bệnh nhân.
Cấu trúc bảng
patients
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| dob | date |
| gender | text |
| city | text |
visits
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| patient_id | integer → patients.id |
| visit_date | date |
| department | text |
| fee | numeric(12, 2) |
Truy vấn mẫu
select department, count(*) as so_lan_kham, sum(fee) as tong_phi
from visits
group by department
order by tong_phi desc;Giáo dục
Học viên, khóa học và lượt ghi danh — luyện tỷ lệ hoàn thành, doanh thu khóa học và phân tích hành vi học tập.
Cấu trúc bảng
edu_students
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| name | text |
| signup_date | date |
| country | text |
edu_courses
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| title | text |
| price | numeric(12, 2) |
edu_enrollments
| Cột | Kiểu dữ liệu |
|---|---|
| id | integer |
| student_id | integer → edu_students.id |
| course_id | integer → edu_courses.id |
| enrolled_on | date |
| completed_on | date |
Truy vấn mẫu
select c.title, count(e.student_id) as so_hoc_vien
from edu_courses c
left join edu_enrollments e on e.course_id = c.id
group by c.title
order by so_hoc_vien desc;Dữ liệu hoạt động thế nào?
- Mỗi lượt chạy bài dùng một bản sao riêng của bộ dữ liệu, nên bạn có thể thoải mái thử nghiệm mà không sợ làm hỏng dữ liệu gốc.
- Dữ liệu được tạo lại từ seed script trước mỗi lượt chạy, vì thế kết quả luôn nhất quán giữa các lần.
- Truy vấn mẫu bên dưới sao chép được vào trang luyện tập và chạy ngay.