SELECT Command
Môn học: Hệ quản trị cơ sơ dữ liệu [Buổi 3-4]
GV: Nguyễn Mai Huy
Query Data
SELECT – FROM – WHERE - ORDER
Faculty of Information Technology
SELECT – FROM – WHERE – ORDER BY
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1, column2, ... [ASC | DESC];
Note:
+ Sử dụng từ khóa ASC để sắp tăng dần, DESC để sắp giảm dần
+ Có thể chỉ ra nhiều cột để quy định mức độ ưu tiên sắp xếp khi trùng dữ liệu
FROM WHERE SELECT ORDER BY
Nguyễn Mai Huy – Master of Information technology
Filter data
Expression & Operators
Faculty of Information Technology
AND, OR and NOT Operators
SELECT * FROM Customers WHERE Country='France' And City='Paris';
SELECT CustomerID, CustomerName,ContactName,Address,City,PostalCode,Country
FROM Customers
WHERE City='Paris' or City='Marseille' or City='Lyon';
Nguyễn Mai Huy – Master of Information technology
AND, OR and NOT Operators
SELECT *
FROM Customers
WHERE Country = 'France' And NOT City='Paris';
Nguyễn Mai Huy – Master of Information technology
Between … And Operators
SELECT *
FROM Products
WHERE Price Between 40 And 50;
Nguyễn Mai Huy – Master of Information technology
Like Operators & Wildcard
SELECT *
FROM Customers
WHERE ContactName Like '%phil%';
SELECT CustomerID, CustomerName,ContactName,Address,City,PostalCode,Country
FROM Customers
WHERE CustomerName Like ‘w%';
Nguyễn Mai Huy – Master of Information technology
In Operators
SELECT CustomerID, CustomerName,ContactName,Address,City,PostalCode,Country
FROM Customers
WHERE Country In ('Italy', 'Spain');
Nguyễn Mai Huy – Master of Information technology
In Operators and subquery
SELECT CustomerID, CustomerName,ContactName,Address,City,PostalCode,Country
FROM Customers
WHERE Country In (SELECT Country FROM Suppliers);
Nguyễn Mai Huy – Master of Information technology
Any Operators and subquery
Products [table]
OrderDetails [table]
Nguyễn Mai Huy – Master of Information technology
Any Operators and subquery
SELECT ProductName, Unit, Price
FROM Products
WHERE ProductID = ANY (SELECT ProductID
FROM OrderDetails
WHERE Quantity > 90);
Nguyễn Mai Huy – Master of Information technology
Exist Operators and subquery
Products [table]
Suppliers [table]
Nguyễn Mai Huy – Master of Information technology
Exist Operators and subquery
SELECT SupplierName, Address, City, Country
FROM Suppliers
WHERE EXISTS (SELECT ProductName
FROM Products
WHERE Products.SupplierID = Suppliers.supplierID AND Price = 22);
Nguyễn Mai Huy – Master of Information technology
Exist Operators and subquery
SELECT SupplierName, Address, City, Country
FROM Suppliers s
WHERE EXISTS (SELECT ProductName
FROM Products p
WHERE (p.SupplierID = s.supplierID) AND (Price between 18 and 20));
Nguyễn Mai Huy – Master of Information technology
Case statement
Like an IF-THEN-ELSE statement in the others program language
Nguyễn Mai Huy – Master of Information technology
Case statement
SELECT OrderID, Quantity, CASE WHEN Quantity > 30 THEN 'Sold to agents'
WHEN Quantity = 30 THEN 'Quantity is 30'
ELSE 'Retail'
END AS Notification
FROM OrderDetails;
Nguyễn Mai Huy – Master of Information technology
Distinct in Query result
SELECT DISTINCT Country FROM Customers;
Nguyễn Mai Huy – Master of Information technology
SQL Joins
Aggregate data from multiple tables
Faculty of Information Technology
JOIN clause
A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.
Different Types of SQL JOINs
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right
table
Nguyễn Mai Huy – Master of Information technology
SQL JOIN
Nguyễn Mai Huy – Master of Information technology
INNER JOIN
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Nguyễn Mai Huy – Master of Information technology
INNER JOIN
SELECT C.CustomerName, C.Address, C.City, O.OrderID, O.OrderDate
FROM Orders O
INNER JOIN Customers C ON (O.CustomerID = C.CustomerID);
Nguyễn Mai Huy – Master of Information technology
INNER JOIN :: Multi-table
SELECT C.CustomerName, C.Address, C.City, O.OrderDate, P.ProductName,
P.Price, D.Quantity, D.Quantity*P.Price as Value
FROM Orders O INNER JOIN Customers C ON (O.CustomerID = C.CustomerID)
INNER JOIN OrderDetails D ON (O.OrderID = D.OrderID)
INNER JOIN Products P ON (P.ProductID = D.ProductID)
WHERE O.OrderID = 10248
Nguyễn Mai Huy – Master of Information technology
UNION
The UNION operator is used to combine the result-set of two or more
SELECT statements.
Each SELECT statement within UNION must have the same number of
columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
Nguyễn Mai Huy – Master of Information technology
UNION City from Customers and Supplies
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Nguyễn Mai Huy – Master of Information technology
Statistics table
Group by and Having in SELECT
Faculty of Information Technology
Group by clause
Mệnh đề GROUP BY trong SQL được sử dụng để sắp xếp những dữ liệu giống hệt
nhau thành các nhóm nhằm phục vụ cho nhu cầu thống kê dựa trên tần suất xuất
hiện của dữ liệu trên bảng. Như vậy, nếu một cột cụ thể có cùng giá trị trong các
hàng khác nhau thì nó sẽ sắp xếp các hàng này thành một nhóm.
Lưu ý:
Mệnh đề GROUP BY được sử dụng trong câu lệnh SELECT.
Trong truy vấn, mệnh đề GROUP BY được đặt sau mệnh đề WHERE.
Mệnh đề GROUP BY được đặt trước mệnh đề ORDER BY.
Nguyễn Mai Huy – Master of Information technology
Group by clause
Nguyễn Mai Huy – Master of Information technology
Group by :: Syntax
FROM WHERE SELECT Group BY ORDER BY
Nguyễn Mai Huy – Master of Information technology
Group By :: Example
SELECT Country, COUNT(CustomerID)
As 'The Number of customers'
FROM Customers
GROUP BY Country;
Nguyễn Mai Huy – Master of Information technology
Group By :: Example
SELECT Country, COUNT(CustomerID)
As 'The Number of customers'
FROM Customers
GROUP BY Country;
SELECT Country, COUNT(CustomerID)
As 'The Number of customers'
FROM Customers
GROUP BY Country
Order by COUNT(CustomerID) DESC;
Nguyễn Mai Huy – Master of Information technology
Group By With JOIN
Orders table
Shippers table
Nguyễn Mai Huy – Master of Information technology
Group By :: Example
SELECT S.ShipperName, COUNT(O.OrderID) AS Number_Of_Orders
FROM Orders O LEFT JOIN Shippers S ON O.ShipperID = S.ShipperID
GROUP BY S.ShipperName;
Nguyễn Mai Huy – Master of Information technology
Having :: Syntax
FROM WHERE SELECT Group BY HAVING ORDER BY
Nguyễn Mai Huy – Master of Information technology
Having :: Function
AVG(expression) Calculate the average of the expression.
COUNT(expression) Count occurrences of non-null values returned by the expression.
COUNT(*) Counts all rows in the specified table.
MIN(expression) Finds the minimum expression value.
MAX(expression) Finds the maximum expression value.
SUM(expression) Calculate the sum of the expression.
Nguyễn Mai Huy – Master of Information technology
Having :: Example
SELECT E.LastName, COUNT(O.OrderID) AS Number_Of_Orders
FROM Orders O INNER JOIN Employees E ON O.EmployeeID = E.EmployeeID
GROUP BY E.LastName
HAVING COUNT(O.OrderID) > 10;
Nguyễn Mai Huy – Master of Information technology
Having :: Example
SELECT E.LastName, COUNT(O.OrderID) AS Number_Of_Orders
FROM Orders O INNER JOIN Employees E ON O.EmployeeID = E.EmployeeID
WHERE LastName Like 'D%' OR LastName Like 'L%'
GROUP BY E.LastName
HAVING COUNT(O.OrderID) >= 25;
Nguyễn Mai Huy – Master of Information technology
Nhớ gì ?!!!
Cho chế thực thi truy vấn thông tin của SELECT; Các phép
toán & ký tự đại diện dùng cho truy vấn dữ liệu
Phép JOIN đối với việc truy vấn thông tin có ý nghĩa gì, có
bao nhiêu hình thức JOIN,
Thống kê dữ liệu trên bảng; Group by & Having
Nguyễn Mai Huy – Master of Information technology
Tài liệu tham khảo
Itzik Ben-Gan, “Microsoft® SQL Server ® 2012 T-SQL Fundamentals”,
O’Reilly Media Inc, 2012
Itzik Ben-Gan, Dejan Sarka, Ed Katibah, Greg Low, Roger Wolter, and Isaac
Kunen, “Inside Microsoft SQL Server 2008: T-SQL Programming”,
Microsoft Press, 2010
w3schools, “Introduction to SQL”,
https://www.w3schools.com/sql/sql_intro.asp, 10:54PM, 18/06/2020
Microsoft SQL Server Tutorial, “Tutorials for SQL Server”,
https://docs.microsoft.com/en-us/sql/sql-server/tutorials-for-sql-server-2016,
10:54 PM, 18/06/2020
Nguyễn Mai Huy – Master of Information technology