0% found this document useful (0 votes)
3 views9 pages

CSDL

The document contains a series of SQL queries and commands for managing databases related to customers, orders, products, and flight management systems. It includes commands for creating tables, inserting data, and performing various queries to retrieve and manipulate data. Additionally, it demonstrates the use of joins, filters, and aggregate functions in SQL.

Uploaded by

thanhlbinh1804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

CSDL

The document contains a series of SQL queries and commands for managing databases related to customers, orders, products, and flight management systems. It includes commands for creating tables, inserting data, and performing various queries to retrieve and manipulate data. Additionally, it demonstrates the use of joins, filters, and aggregate functions in SQL.

Uploaded by

thanhlbinh1804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

use NorthWind

go
select *
From Customers, Orders
--Câu2
select *
From Customers
inner join Orders
on Customers.CustomerID = Orders.CustomerID
--Câu3
select *
from Customers
where Customers.City = N'London' or Customers.City= N'France'
order by Customers.Country desc
--Câu 4
Select *
from Customers
where
(Customers.ContactTitle = N'Sales Manager' and Customers.Country =
N'USA')

or (
Customers.Country = N'Mexico' and Customers.ContactTitle = N'Owner')
order by Customers.Country desc
--Câu 5
select Orders.OrderID, Orders.OrderDate,
Customers.CompanyName, Employees.LastName
from Orders
inner join Customers
on Orders.CustomerID = Customers.CustomerID
inner join Employees
on Orders.EmployeeID = Employees.EmployeeID
where Orders.OrderDate between '1997-01-01' and {d'1997-06-30'}
--Câu 6
select Orders.OrderID, Orders.OrderDate,
Customers.CompanyName, Employees.LastName
from Orders
inner join Customers
on Orders.CustomerID = Customers.CustomerID
inner join Employees
on Orders.EmployeeID = Employees.EmployeeID
where (MONTH(Orders.OrderDate) = 2 and YEAR(Orders.OrderDate) = 1997)
or (Orders.OrderDate between '1997-03-01' and {d'1997-03-30'});
--Câu 7
select Orders.OrderID, Orders.OrderDate,
Customers.CompanyName, Employees.LastName
from Orders
inner join Customers
on Orders.CustomerID = Customers.CustomerID
inner join Employees
on Orders.EmployeeID = Employees.EmployeeID
where --DAY(Orders.OrderDate) %2 = 0 and YEAR(Orders.OrderDate) %2 != 0
DAY(Orders.OrderDate) in ( 15, 25, 16, 05, 01) and YEAR(Orders.OrderDate) in
(199, 1996)
--Câu12
select *
from Products
inner join Suppliers
on Products.SupplierID = Suppliers.SupplierID
inner join Categories
on Products.CategoryID = Categories.CategoryID
where Products.ProductName Like N'%ch%' -- Lấy kí tự
--Cau 17
select
Orders.OrderID, Customers.CompanyName,
Employees.LastName, Orders.OrderID,
Orders.RequiredDate,Orders.OrderDate, Orders.ShipCountry,
Orders.Freight
from Customers
inner join Orders
on Orders.CustomerID = Customers.CustomerID
inner join Employees
on Orders.EmployeeID = Employees.EmployeeID
where
--cast(Orders.OrderDate as date) = cast(getdate() as date) OR
--cast(Orders.RequiredDate as date) = cast(getdate() as date);
cast(Orders.OrderDate as date) = {d'1996-10-01'} or
cast(Orders.RequiredDate as date) = {d'1996-10-30'};
--câu 19 Cách 2
select

[Order Details].UnitPrice, [Order Details].Quantity,


AnotherOrderDetailToTal.OrderID, Products.ProductName,
AnotherOrderDetailToTal.ThanhTien,
[Order Details].Discount,
AnotherOrderDetailToTal.TienGiamGia,
(AnotherOrderDetailToTal.ThanhTien-AnotherOrderDetailToTal.TienGiamGia)
as TienPhaiTra
from Products
inner join (
select
[Order Details].OrderID,
[Order Details].ProductID,
([Order Details].UnitPrice *[Order Details].Quantity)
as ThanhTien,
([Order Details].UnitPrice *[Order
Details].Quantity)*[Order Details].Discount as TienGiamGia
from [Order Details]
inner join Orders
on [Order Details].OrderID= Orders.OrderID
where YEAR(Orders.OrderDate)=1997 and [Order
Details].Discount > 0 and
(([Order Details].UnitPrice *[Order Details].Quantity)-
([Order Details].UnitPrice *[Order Details].Quantity)*[Order
Details].Discount) <50
) AnotherOrderDetailToTal
on AnotherOrderDetailToTal.ProductID =
Products.ProductID
inner join [Order Details]
on AnotherOrderDetailToTal.OrderID = [Order
Details].OrderID and
AnotherOrderDetailToTal.ProductID = [Order
Details].ProductID

order by [Order Details].OrderID;


--Cau21
Select Products.ProductID, Products.ProductName,
Suppliers.CompanyName,
Products.UnitPrice, Products.UnitsInStock,
Products.UnitPrice*Products.UnitsInStock as TriGia
From Products
inner join Suppliers
on Products.SupplierID = Suppliers.SupplierID
where (
Products.Discontinued = 1 and
Products.UnitsInStock >0)
--Cau22
select Employees.HireDate,
Employees.TitleOfCourtesy + N' ' + Employees.LastName + N' '
+Employees.FirstName as HoTen,
Employees.Title, Employees.BirthDate, Employees.HomePhone
From Employees

where Year(Employees.HireDate) < 1993


--Cau23
select
Count(Products.ProductID) as SoLuong,
Max(Products.UnitPrice) as DonGiaCaoNhat,
Min(Products.UnitPrice) as DonGiaThapNhat,
AVG(Products.UnitPrice) as DonGiaTrungBinh
From Products
select *
From Products
--Cau24
select
Categories.CategoryID, Categories.CategoryName,
Count(Products.ProductID) as SoLuong,
Max(Products.UnitPrice) as DonGiaCaoNhat,
Min(Products.UnitPrice) as DonGiaThapNhat,
AVG(Products.UnitPrice) as DonGiaTrungBinh
From Products
inner join Categories
on Products.CategoryID = Categories.CategoryID
Group By
Categories.CategoryID,
Categories.CategoryName
--Cau25
select COUNT([Order Details].OrderID) as TongDon
From Orders
inner join [Order Details]
on Orders.OrderID = [Order Details].OrderID
where Orders.ShipCountry in ('Belgium', 'Canada', 'UK')
--Cau26
select Orders.ShipCountry, COUNT([Order Details].OrderID) as TongDon
From Orders
inner join [Order Details]
on Orders.OrderID = [Order Details].OrderID
where Orders.ShipCountry in ('Belgium', 'Canada', 'UK')
Group By Orders.ShipCountry
--Cau27
select Categories.CategoryID, Categories.CategoryName,
AVG(Products.UnitPrice) as DonGiaTrungBinh

From Products
inner join Categories
on Products.CategoryID= Categories.CategoryID
Group By
Categories.CategoryID, Categories.CategoryName
HAVING
AVG(Products.UnitPrice) >30
--Cau28
select Categories.CategoryID, Categories.CategoryName,
AVG(Products.UnitPrice) as DonGiaTrungBinh

From Products
inner join Categories
on Products.CategoryID= Categories.CategoryID
where (Products.UnitPrice) >40
Group By
Categories.CategoryID, Categories.CategoryName
--Cau31
select top 15
Products.ProductID,
Products.ProductName,
Products.UnitPrice
From Products
Order by Products.UnitPrice Desc
--Cau32
select top 5 Percent
Products.ProductID,
Products.ProductName,
Products.UnitPrice
From Products
Order by Products.UnitPrice Desc;
--câu33
select top 10
Customers.CustomerID, Customers.CompanyName,
COUNT(Orders.OrderID) as SoLuongDonHang
from Customers
inner join Orders
on Customers.CustomerID = Orders.CustomerID
group by
Customers.CustomerID, Customers.CompanyName
order by COUNT(Orders.OrderID) desc
--cach2
Select Customers.CustomerID, Customers.CompanyName,
KhachHangMuaDonHang.SoLuongDonHang
From Customers
inner join (

select
top 10
Orders.CustomerID,
COUNT(Orders.OrderID) as SoLuongDonHang
from Orders
Group by Orders.CustomerID
order by COUNT(Orders.OrderID) desc

)KhachHangMuaDonHang
on KhachHangMuaDonHang.CustomerID = Customers.CustomerID
--Cau34
select Customers.*
From Customers
where Customers.CustomerID in
(
select distinct Orders.CustomerID
from Orders
)
--cách 2
select distinct Customers.*
From Customers
inner join Orders
on Customers.CustomerID = Orders.CustomerID

use master
go
create database QuanLiChuyenBay
go
begin
use QuanLiChuyenbay;
end
go

create table MayBay(


MaMB nvarchar(10) not null primary key,
HangSX nvarchar(50) null,
NamSX nvarchar(50) null,
SoHieu nvarchar(20) null,
SoChoNgoi nvarchar(10) null,
);
create table HanhKhach(
MaHK nvarchar(10) not null primary key,
HoTen nvarchar(20) null,
NamSinh date null,
);
create table TuyenBay(
MaTB nvarchar(10) not null primary key,
DiemDi nvarchar(20) null,
DiemDen nvarchar(20) null,
);
create table Chuyenbay (
MaCB nvarchar(10) not null primary key,
MaTB nvarchar(10) null,
Constraint fk_CB_TB foreign key (MaTB) references TuyenBay(MaTB)
);
create table TiepVien(
MaTV nvarchar(10) not null primary key,
HoTen nvarchar(50) null,
);
create table PhiCong(
MaPC nvarchar(10) not null primary key,
HoTen nvarchar(50) null,
);
create table ThucHienChuyenBay(
MaTHCB nvarchar(10) not null primary key,
MaMB nvarchar(10) null,
MaCB nvarchar(10) null,
NgayGiohacanh date null,
Constraint fk_THCB_CB foreign key (MaCB) references ChuyenBay(MaCB),
Constraint fk_THCB_MayBay foreign key (MaMB) references MayBay(MaMB),
);
create table HanhKhach_ChuyenBay(
MaTHCB nvarchar(10) not null,
MaHK nvarchar(10) not null,
Constraint pk_HK_CB primary key (MaTHCB, MaHK),
Constraint fk_HK_CB foreign key (MaHK) references HanhKhach(MaHK),
Constraint fk_HK_CB_THCB foreign key (MaTHCB) references
ThucHienChuyenBay(MaTHCB)
);
create table PhiCong_CB(
MaTHCB nvarchar(10) not null,
MaPC nvarchar(10) not null,
Constraint pk_PC_CB primary key (MaTHCB, MaPC),
Constraint fk_PC_CB_PC foreign key (MaPC) references PhiCong(MaPC),
Constraint fk_PC_CB_THCB foreign key (MaTHCB) references
ThucHienChuyenBay(MaTHCB),
);

alter table PhiCong_CB drop constraint fk_PC_CB_PC


alter table PhiCong_CB add constraint fk_PC_CB_PC foreign key (MaPC)
references PhiCong(MaPC)
drop table PhiCong

use master
go
--create database QuanliBanhang
--go

begin
use QuanLiBanHang;
end
go
create table KhachHang(
MaKH nvarchar(10) not null primary key,
HoTenKH nvarchar(50) null,
GioiTinh nvarchar(10) null,
SoDienThoaiKH bigint null,

);
create table NhanVien(
MaNV nvarchar(10) not null primary key,
HoTenNV nvarchar(50) null,
GioiTinh nvarchar(10) null,
SoDienThoaiNV bigint null,

);
create table DonHang(
MaDH nvarchar(10) not null primary key,
NgayMua date null,
TienVC money null,
MaNV nvarchar(10) null,
MaKH nvarchar(10) null,
Constraint fk_DonHang_KhachHang foreign key (MaKH) references
KhachHang(MaKH),
Constraint fk_DonHang_NhanVien foreign key (MaNV) references
NhanVien(MaNV),

);

create table HangHoa(


MaHH nvarchar(10) not null primary key,
TenHH nvarchar(50) null,
DonViTinh nvarchar(20) null,
DonGiaNiemYet money null,

);
create table ChiTietDonHang(
MaDH nvarchar(10) not null,
MaHH nvarchar(10) not null,
Constraint pk_ChiTietDonHang primary key (MaDH, MaHH),
DonGia nvarchar(50) null,
SoLuongBan nvarchar(50) null,
Constraint fk_ChiTiet_DonHang foreign key (MaDH) references
DonHang(MaDH),
Constraint fk_CHiTiet_HangHoa foreign key (MaHH) references
HangHoa(MaHH),
);
ALTER TABLE DonHang DROP CONSTRAINT fk_DonHang_KhachHang;
ALTER TABLE DonHang DROP CONSTRAINT fk_DonHang_NhanVien;
alter table ChiTietDonHang drop constraint fk_ChiTiet_DonHang;
drop table DonHang

create table SinhVien


(
MaSV nvarchar(10) not null,
HoLotSV nvarchar(50) null,
TenSV nvarchar(50) null,
GioiTinhSV nvarchar(20) null constraint df_gioi_tinh_sv_m
default 'M',-- default 'F',-- check(GioiTinhSV in ('M', 'F')),
GhiChuSV nvarchar_max,
EmailSinhVien nvarchar(100) null,
MaLop nvarchar(10) null,
constraint ck_gioi_tinh_sv check(GioiTinhSV in ('M', 'F')),
constraint uq_email_sinh_vien unique(EmailSinhVien)
);
create table GiangVien
(
MaGiangVien nvarchar(10) not null primary key,
HoLotGiangVien nvarchar(100) null,
TenGiangVien nvarchar(50) null,
GioiTinhGiangVien nvarchar(20) null check (GioiTinhGiangVien
in ('F', 'M')),
EmailGiangVien nvarchar(100) null unique
);
create table Diem
(
MaSV nvarchar(10) not null,
MaMonHoc nvarchar(10) not null,
MaGiangVien nvarchar(10) not null,
Diem decimal(14,7) null,
constraint pk_diem primary key (MaSV, MaMonHoc, MaGiangVien),
constraint fk_diem_sinh_vien foreign key (MaSV) references
SinhVien (MaSV),
constraint fk_diem_mon_hoc foreign key (MaMonHoc) references
MonHoc (MaMonHoc),
constraint fk_diem_giang_vien foreign key (MaGiangVien)
references GiangVien (MaGiangVien)
);
begin
if OBJECT_ID(N'uq_ten_khoa', N'UQ') is null
begin
alter table Khoa
add constraint uq_ten_khoa unique(TenKhoa);
end

if COL_LENGTH(N'QuanLySinhVien.dbo.Khoa', N'SoDienThoaiKhoa') is null


begin
alter table Khoa
add SoDienThoaiKhoa nvarchar(20) null;
end
end
begin

if OBJECT_ID(N'df_gioi_tinh_sv_m', N'D') is not null


begin
alter table SinhVien
drop constraint df_gioi_tinh_sv_m;
end

if OBJECT_ID(N'df_gioi_tinh_sv_f', N'D') is null


begin
alter table SinhVien
add constraint df_gioi_tinh_sv_f default 'F' for
GioiTinhSV;
end
end

You might also like