You are on page 1of 35

SQL Part 1

Lê Hồng Hải
Khoa CNTT-ĐHQGHN
Email: hailh@vnu.edu.vn
Nội dung chi tiết
 Giới thiệu SQL
 Môi trường thực hành
 Truy vấn dữ liệu
- SELECT
 Sắp xếp dữ liệu
- ORDER BY
 Lọc dữ liệu
- WHERE, AND, OR, IN, BETWEEN, LIKE, LIMIT, IS NULL
 Nối bảng dữ liệu
- Joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, Self-join

Cơ sở dữ liệu - Khoa CNTT 2


Giới thiệu SQL
 SQL stands for Structured Query Language
 SQL lets you access and manipulate databases

Cơ sở dữ liệu - Khoa CNTT 3


RDBMS ~ SQL

Cơ sở dữ liệu - Khoa CNTT 4


Relational Database Management
System
 RDBMS stands for Relational Database
Management System.
 RDBMS is the basis for SQL, and for all modern
database systems such as MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access.
 The data in RDBMS is stored in database objects
called tables

Cơ sở dữ liệu - Khoa CNTT 5


Thực hành ngôn ngữ SQL
 Cài đặt MySQL
- https://www.mysqltutorial.org/install-mysql/
 CSDL mẫu:
- https://www.mysqltutorial.org/mysql-sample-
database.aspx
 Thực hành online:
- https://www.mysqltutorial.org/tryit/

Cơ sở dữ liệu - Khoa CNTT 6


CSLD Mẫu

Cơ sở dữ liệu - Khoa CNTT 7


CSDL Mẫu
 Customers: stores customer’s data.
 Products: stores a list of scale model cars.
 ProductLines: stores a list of product line categories.
 Orders: stores sales orders placed by customers.
 OrderDetails: stores sales order line items for each
sales order.
 Payments: stores payments made by customers based
on their accounts.
 Employees: stores all employee information as well as
the organization structure such as who reports to whom.
 Offices: stores sales office data.

Cơ sở dữ liệu - Khoa CNTT 8


1. Truy vấn dữ liệu - SELECT
 SELECT lastName
FROM employees;

Cơ sở dữ liệu - Khoa CNTT 9


Truy vấn dữ liệu - SELECT
SELECT
lastname,
firstname,
jobtitle
FROM
employees;

Cơ sở dữ liệu - Khoa CNTT 10


Trả về tất cả các cột

SELECT
*
FROM
employees;

Cơ sở dữ liệu - Khoa CNTT 11


2. Sắp xếp dữ liệu – ORDER BY
 Cú pháp: SELECT
select_list
FROM
table_name
ORDER BY
column1 [ASC|DESC],
column2 [ASC|DESC], ...;
Tham khảo:
https://www.mysqltutorial.org/mysql-order-by/

Cơ sở dữ liệu - Khoa CNTT 12


Ví dụ: sắp xếp dữ liệu theo tên

SELECT
contactLastname,
contactFirstname
FROM
customers
ORDER BY
contactLastname;

Cơ sở dữ liệu - Khoa CNTT 13


Sắp xếp theo nhiều cột

SELECT
contactLastname,
contactFirstname
FROM
customers
ORDER BY
contactLastname,
contactFirstname;

Cơ sở dữ liệu - Khoa CNTT 14


Sắp xếp dữ liệu biểu thức
SELECT
orderNumber,
orderLineNumber,
quantityOrdered * priceEach
AS subtotal
FROM
orderdetails
ORDER BY subtotal DESC;

Cơ sở dữ liệu - Khoa CNTT 15


3. Lọc dữ liệu
 WHERE
 AND, OR
 IN, BETWEEN
 LIKE
 LIMIT
 IS NULL

Cơ sở dữ liệu - Khoa CNTT 16


WHERE
 Xác định điều kiện tìm kiếm dữ
liệu cho câu truy vấn

SELECT
select_list
FROM
table_name
WHERE
search_condition

Cơ sở dữ liệu - Khoa CNTT 17


Ví dụ:
SELECT
lastname,
firstname,
jobtitle
FROM
employees
WHERE
jobtitle = 'Sales Rep';

Cơ sở dữ liệu - Khoa CNTT 18


Một số toán tử sử dụng

Cơ sở dữ liệu - Khoa CNTT 19


Trình tự đánh giá WHERE

Cơ sở dữ liệu - Khoa CNTT 20


Một số ví dụ sử dụng
 AND, OR
 IN, BETWEEN
 LIKE
 LIMIT
 IS NULL (không dùng = NULL)

Tham khảo:
https://www.mysqltutorial.org/mysql-where/

Cơ sở dữ liệu - Khoa CNTT 21


4. Nối bảng dữ liệu
 CSDL Quan hệ bao gồm các bảng có liên kết với nhau
thông qua cột chung (còn gọi là khóa ngoại – foreign key)
 Ví dụ: Bảng orders và bảng orderdetails liên kết sử dụng
cột orderNumber:

Cơ sở dữ liệu - Khoa CNTT 22


CSDL Classic models

Cơ sở dữ liệu - Khoa CNTT 23


MySQL hỗ trợ các kiểu Join sau

 Inner join
 Left join
 Right join
 Cross join

Cơ sở dữ liệu - Khoa CNTT 24


MySQL INNER JOIN
 INNER JOIN nối mỗi dòng của 1 bảng với các
dòng của bảng kia, cho phép truy vấn các dòng
chứa các cột có dữ liệu giống nhau từ các bảng

SELECT select_list
FROM t1 INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2 ...;

https://www.mysqltutorial.org/mysql-inner-join.aspx

Cơ sở dữ liệu - Khoa CNTT 25


Ví dụ: thêm thông tin
textDescription từ productlines

SELECT
productCode,
productName,
textDescription
FROM
products t1
INNER JOIN productlines t2
ON t1.productline = t2.productline;

Cơ sở dữ liệu - Khoa CNTT 26


Sử dụng USING khi các cột nối
cùng tên

SELECT productCode, productName, textDescription


FROM products
INNER JOIN productlines USING (productline);

Cơ sở dữ liệu - Khoa CNTT 27


INNER JOIN clauses to join three
tables: orders, orderdetails,
and products

Cơ sở dữ liệu - Khoa CNTT 28


Ví dụ nối sử dụng 3 bảng
SELECT
orderNumber,
orderDate,
orderLineNumber,
productName,
quantityOrdered,
priceEach
FROM
orders
INNER JOIN
orderdetails USING (orderNumber)
INNER JOIN
products USING (productCode)

Cơ sở dữ liệu - Khoa CNTT 29


LEFT JOIN
 LEFT JOIN trả về tất cả các cột của bảng bên trái,
trong trường hợp không có giá trị nối bên phải, các
cột của các dòng dữ liệu của bảng bên phải sẽ có
giá trị NULL

Cơ sở dữ liệu - Khoa CNTT 30


Ví dụ:
SELECT
customers.customerNumber,
customerName,
orderNumber,
status
FROM
customers
LEFT JOIN orders ON
orders.customerNumber = customers.customerNumber;

Cơ sở dữ liệu - Khoa CNTT 31


SELF-JOIN
 Một bảng nối với chính bảng đó

Cơ sở dữ liệu - Khoa CNTT 32


SELF-JOIN
SELECT
CONCAT(m.lastName, ', ', m.firstName)
AS Manager,
CONCAT(e.lastName, ', ', e.firstName)
AS 'Direct report'
FROM
employees e
INNER JOIN employees m ON
m.employeeNumber = e.reportsTo

Cơ sở dữ liệu - Khoa CNTT 33


Phần 1 SQL
 Truy vấn dữ liệu
- SELECT
 Sắp xếp dữ liệu
- ORDER BY
 Lọc dữ liệu
- WHERE, AND, OR, IN, BETWEEN, LIKE, LIMIT, IS
NULL
 Nối bảng dữ liệu
- Joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, Self-join

Cơ sở dữ liệu - Khoa CNTT 34


Cơ sở dữ liệu - Khoa CNTT 35

You might also like