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

SQL

The document contains examples of SQL queries including select, update, insert, delete and cross tab queries. The queries are selecting, updating, inserting or deleting data from tables in a database to retrieve, modify or aggregate the data. Some queries are joining multiple tables and filtering on conditions.

Uploaded by

2251012156vi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL

The document contains examples of SQL queries including select, update, insert, delete and cross tab queries. The queries are selecting, updating, inserting or deleting data from tables in a database to retrieve, modify or aggregate the data. Some queries are joining multiple tables and filtering on conditions.

Uploaded by

2251012156vi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

--MAKE - TABLE QUERY --

--1--
select * into CacKhachHangMy
from Customers
where Country = 'USA'
--2--
select top(5) Employees.EmployeeID as 'Ma nhan vien', FirstName + ' '
+ LastName as 'Ho va ten', count(OrderID) as 'Tong so don hang'
INTO TIM5NHANVIENGIOI
from Employees,Orders
where Employees.EmployeeID = Orders.OrderID
group by Employees.EmployeeID , FirstName + ' '+ LastName
Order by 3 desc
--3--
select top(10) Customers.CustomerID, CompanyName, Address +' '+City +' ' +
Country as 'Diachi', count(OrderID)
into Tim10KhachHang
from Customers,Orders
where Customers.CustomerID = Orders.CustomerID
group by Customers.CustomerID, CompanyName, Address +' '+City +' ' +Country
order by 4 desc
--4--
Select top(5) Country, Count ('Order Details'.ProductID) 'tong san pham'
into Top5QGMuaHang
from Customers , Products , 'Order Details' , Orders
where Products.ProductID ='Order Details'. ProductID
and Customers.CustomerID = Orders.CustomerID
and [Order Details].OrderID = Orders.OrderID
group by Country
Order by 2 DESC
--5--
Select top(5) Country, Count ('Order Details'.ProductID) 'tong san pham'
into Top5QGMuaHang
from Customers , Products , 'Order Details' , Orders
where Products.ProductID ='Order Details'. ProductID
and Customers.CustomerID = Orders.CustomerID
and [Order Details].OrderID = Orders.OrderID
group by Country
Order by 2 ASC
-- UPDATE QUERY --
--1--
Update Customers
set Country = 'My'
where Country = 'USA'
--2--
Update Customers set Country = 'Duc' where Country = 'Germany'
Update Customers set Country = 'Phap' where Country = 'France'
--3--
Update Customers set Country = 'Duc' where Country = 'Germany'
Update Customers set Country = 'Phap' where Country = 'France'
Update Suppliers set Country = 'Duc' where Country = 'Germany'
Update Suppliers set Country = 'Phap' where Country = 'France'
--4--
Update LOAISANPHAM set ContactName = 'nuoc giai khat' where ContactName = 'Beverages'
Update LOAISANPHAM set ContactName = 'gia vi' where ContactName = 'Condiments'
--5--
Update LOAISANPHAM set ContactName = 'banh keo' where ContactName = 'Confections'
Update LOAISANPHAM set ContactName = 'hai san' where ContactName = 'Seafood'
--6--
Update Customers set PostalCode = '18'
where PostalCode = '2' and Country = 'Germany'
-- APPEND QUERY --
--1--
insert into Categories (CategoryName, Description)
values ('Van phong pham' , 'Sach, vo, giay, but, muc')
--2--
use Northwind
go
CREATE PROCEDURE dbo.Cau2 @CategoryName nvarchar (15), @Description nvarchar(30)
AS
insert into Customers (CategoryName, Description)
values(@CategoryName, @Description )
GO
Exec Cau2 @CategoryName = ' ' , @Description = ' '
--3--
insert into Customers(CompanyName, ContactName, ContactTitle, Phone, Address, City, Country)
values ('OU.HCM', 'Kim Anh', 'Phu tho hoa', '0340213456' , 'quan 7', 'sai gon', 'Viet Nam')
--4--
insert into Employees(LastName, FirstName, Address, City,Country)
values ('vi','vu','quan 7','sai gon','viet nam')
--5--
DECLARE @LastName nvarchar(20)
DECLARE @FirstName nvarchar(10)
DECLARE @Address nvarchar(60)
DECLARE @City nvarchar(15)
DECLARE @Country nvarchar(15)

SET @LastName = 'AAAAA'


SET @FirstName = 'AAAAA'
SET @Address = 'AAAAA'
SET @City = 'AAAAA'
SET @Country ='AAAAA'

insert into Employees(LastName, FirstName, Address, City, Country)


values (@LastName, @FirstName, @City, @Country)
--APPEND QUERY THEM NHIEU HANG DU LIEU --
--6--
insert into KhachHang
select * from Customers
where Country = 'My'
--7--
insert into KhachHang
select * from Customers
where CustomerID in (select top 10 Customers.CustomerID
from Customers, Orders
where Customers.CustomerID = Orders.CustomerID
group by Customers.CustomerID ORDER BY COUNT(*) DESC)
and CustomerID not in (select CustomerID from KhachHang)
--DELETE QUERY--
--1--
delete from Categories
where CategoryName = 'Van phong pham'
--2--
delete from Customers
where Country = 'USA'
--3--
use Northwind
delete from Customers
--4--
select * into Customers_Backup from Customers
select * from Customers_Backup
select CustomerID from Orders
select distinct CustomerID from Orders
delete from Customers_Backup
where CustomerID not in(select distinct CustomerID from Orders)
select * from Customers_Backup
--5--
select * into Categories_Backup from Categories
select * from Categories_Backup
select CategoryID
select distinct CustomerID from Orders
delete from Customers_Backup
where CustomerID not in(select distinct CustomerID from Orders)
select * from Customers_Backup

--cross- tab--
--1--
use Northwind
select year(OrderDate), Products.ProductName, sum( Quantity) as 'UnitSold'
from Orders, Products, [Order Details]
where Products.ProductID = [Order Details]. ProductID and [Order Details].OrderID =Orders.OrderID
group by year (OrderDate), Products.ProductName
SELECT ProductName,[1996],[1997],[1998]
FROM
(select year(OrderDate) as 'Years',ProductName, sum(Quantity) as 'UnitSold'
from Orders, Products,[Order Details]
where Products.ProductID = [Order Details].ProductID and [Order Details].OrderID = Orders.OrderID
group by year (OrderDate), Products.ProductName
) ps
PIVOT
(SUM (ps.UnitSold)
FOR ps.Years IN ([1996],[1997],[1998])
) as pvt

--2--
use Northwind
select month(OrderDate),YEAR(OrderDate), Products.ProductName, sum(Quantity) as 'UnitSold'
from Orders, Products, [Order Details]
where Products.ProductID = [Order Details].ProductID and [Order Details].OrderID = Orders.OrderID and year(OrderDate) =
'1997'
group by month(OrderDate),year(OrderDate),Products.ProductName

SELECT ProductName,[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]
FROM
(select MONTH(OrderDate) as 'MONTH',ProductName, sum(Quantity) as 'UnitSold'
from Orders, Products,[Order Details]
where Products.ProductID = [Order Details].ProductID and [Order Details].OrderID = Orders.OrderID
group by MONTH (OrderDate), Products.ProductName
) ps
PIVOT
(SUM (ps.UnitSold)
FOR ps.MONTH IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) as pvt

--3--
use Northwind
select Country, Products.ProductName, sum(Quantity) as 'UnitSold'
from Orders, Products, [Order Details], Customers
where Products.ProductID = [Order Details].ProductID and [Order Details].OrderID = Orders.OrderID
group by Customers.Country, Products.ProductName

SELECT ProductName, [Norway],[Poland],[Portugal],[Spain]


FROM
(select Customers.Country as 'Country' ,ProductName, sum(Quantity) as 'UnitSold'
from Orders, Products,[Order Details],Customers
where Products.ProductID = [Order Details].ProductID and [Order Details].OrderID = Orders.OrderID
group by Customers.Country, Products.ProductName
) ps
PIVOT
(SUM (ps.UnitSold)
FOR ps.Country IN ( [Norway],[Poland],[Portugal],[Spain])
) as pvt

You might also like