Ms SQL PDF
Ms SQL PDF
DQL SELECT
QUERY
DDL CREATE, ALTER, DROP
DATA TYPES
1
04-08-2023
DATABASE
Database
CREATE DATABSE <DATA NAME>
Use <Data Names> DDL WITHOUT
DROP DATABSE <DATA NAME>
CONSTRAINTS
2
04-08-2023
CONSTRAINTS Constraints
3
04-08-2023
Create Table
With
Constraints NOT NULL
UNIQUE Check
Constraint
4
04-08-2023
Default
roll int,
submission char default 'N'
)
INSERT
insert into fordefault (name, roll) values
COMMAND
('abc', 10)
select * from fordefault
5
04-08-2023
DDL
DDL alter table One alter table ABC
add constraint pk_roll primary key (roll); commands: alter column roll int
commands: -- only if its not null
Alter
Add and
Column (for ALTER TABLE fr2
Drop alter table One
alter column emp int not null
drop constraint PK1_roll domain,
Constraint
not null)
6
04-08-2023
UPDATE
(RECORD)
FOREIGN KEY
Statement CONSTRAINT
7
04-08-2023
Order of Execution
From Select
1.SELECT *
DML COMMANDS
2.FROM Person.Password; --<Table Name>
DML :
Select 3.SELECT BusinessEntityID, FirstName, MiddleName, LastName --
Command <Col Name(s)>
4.FROM Person.Person; --<Table Name>;
6.FROM Person.Person;
8
04-08-2023
1.SELECT JobTitle
2.FROM HumanResources.Employee;
1.SELECT NationalIDNumber, HireDate, VacationHours
3.SELECT DISTINCT JobTitle 2.FROM HumanResources.Employee;
4.FROM HumanResources.Employee;
Select with CASE : (when 3.SELECT NationalIDNumber, HireDate, VacationHours,
CASE : (when
CASE
WHEN VacationHours > 70 THEN 'Vacation hours over limit' DML COMMANDS
with WHERE
WHEN VacationHours BETWEEN 48 AND 61 THEN 'Vacation
then else end ) hours average'
ELSE 'Vacation Hours within limit'
END AS VacationHourLimit
FROM HumanResources.Employee;
9
04-08-2023
Order of Execution
From Where Select
SELECT *
1.SELECT StateProvinceID, CountryRegionCode FROM Production.Product;
2.FROM Person. StateProvince
3.WHERE CountryRegionCode <> 'US’; SELECT StandardCost, ListPrice, StandardCost +
Where with Where with ListPrice AS SumListPriceCost
FROM Production.Product
Comparison To compare two columns Arithmetic WHERE SumListPriceCost > 0;
1.SELECT StateProvinceCode, CountryRegionCode
Operators 2.FROM Person. StateProvince
Operators
SELECT StandardCost, ListPrice, StandardCost +
3.WHERE StateProvinceCode <> CountryRegionCode; ListPrice AS SumListPriceCost
FROM Production.Product
WHERE StandardCost + ListPrice > 0;
10
04-08-2023
2.FROM Production.Product
Where with 3.WHERE (ProductID = 800 OR ProductID < 600) AND StandardCost > 3.WHERE Color IS NULL;
50 AND SafetyStockLevel BETWEEN 500 AND 1200; IS NULL/
Logical
IS NOT NULL
Operators 4.SELECT ProductID, Name, StandardCost, ListPrice, SafetyStockLevel 4.SELECT ProductId, Name, Color
5.FROM Production.Product
5.FROM Production.Product
6.WHERE Color IS NOT NULL;
6.WHERE NOT ProductID = 4;
11
04-08-2023
12
04-08-2023
BY DEFAULT ASC IS THE ORDER; Null are considered to be lowest; not necessary
that column on which order by clause is applied is there in select statement too.
with ORDER BY
3.FROM Person.Address
DESC/ASC 4.ORDER BY AddressID;
13
04-08-2023
14
04-08-2023
15
04-08-2023
1.SELECT max(StandardCost)
2.FROM Production.Product
3.HAVING max(standardCost) > 200; 1.SELECT DueDate,Sum(OrderQty) AS TotalOrderPerDueDate
2.FROM Purchasing.PurchaseOrderDetail
GROUP BY 1.SELECT DueDate, Sum(OrderQty) AS TotalOrderPerDueDate GROUP BY 3.WHERE YEAR(DueDate) > 2011 AND Month(DueDate) < 9
with HAVING 2.FROM Purchasing.PurchaseOrderDetail with HAVING 4.GROUP BY DueDate
3.WHERE YEAR(DueDate) > 2011 AND Month(DueDate) < 9 5.HAVING sum(OrderQty) < 6000
4.GROUP BY DueDate 6.ORDER BY DueDate DESC;
5.ORDER BY DueDate DESC;
1.SELECT PurchaseOrderId
2.FROM Purchasing.PurchaseOrderDetail
3.UNION
DML COMMANDS
4.SELECT PurchaseOrderID
5.FROM Purchasing.PurchaseOrderHeader;
OPERATIONS 3.UNION
4.SELECT TaxAmt
5.FROM Purchasing.PurchaseOrderHeader
6.ORDER BY PurchaseOrderDetailID ASC;
16
04-08-2023
1.SELECT PurchaseOrderId
2.FROM Purchasing.PurchaseOrderDetail
3.UNION
4.SELECT PurchaseOrderID
5.FROM Purchasing.PurchaseOrderHeader; 1.SELECT ProductSubCategoryID
2.FROM Production.Product
UNION 1.SELECT PurchaseOrderDetailID INTERSECT 3.INTERSECT
DML COMMANDS
1.SELECT SalesOrderID
2.FROM Sales.SalesOrderDetail
EXCEPT 3.EXCEPT
4.SELECT CustomerID
5.FROM Sales.Customer;
JOINS
17
04-08-2023
1.SELECT pod.PurchaseOrderID,
1.SELECT pod.PurchaseOrderID,
2.pod.PurchaseOrderDetailID,
2.pod.PurchaseOrderDetailID,
3.pod.OrderQty,
3.pod.OrderQty,
4.poh.OrderDate,
JOIN/ INNER
IMPLICIT JOIN 4.poh.OrderDate,
5.poh.ShipDate
5.poh.ShipDate JOIN 6.FROM Purchasing.PurchaseOrderHeader poh
6.FROM Purchasing.PurchaseOrderDetail pod,
7.INNER JOIN
Purchasing.PurchaseOrderHeader poh
8.Purchasing.PurchaseOrderDetail pod
7.WHERE pod.PurchaseOrderID = poh.PurchaseOrderID;
9.ON pod.PurchaseOrderID = poh.PurchaseOrderID
1.SELECT pod.PurchaseOrderID,
1.SELECT p.ProductID, p.Name, pc.ProductCategoryID, pc.Name,
2.pod.PurchaseOrderDetailID,
2.psc.ProductSubCategoryID
3.pod.OrderQty,
4.poh.OrderDate, INNER JOIN 3.FROM Production.Product P
4.INNER JOIN
JOIN/ INNER 5.poh.ShipDate ON MORE 5.Production.ProductSubcategory psc
JOIN + WHERE 6.FROM Purchasing.PurchaseOrderHeader poh THAN TWO 6.ON psc.ProductSubCategoryID = p.ProductSubCategoryID
7.INNER JOIN
TABLES 7.INNER JOIN
8.Purchasing.PurchaseOrderDetail pod
8.Production.ProductCategory pc
9.ON pod.PurchaseOrderID = poh.PurchaseOrderID
9.ON pc.ProductCategoryID = psc.ProductCategoryID;
10.WHERE year(poh.OrderDate) = 2014;
18
04-08-2023
1.SELECT count(*)
2.FROM HumanResources.Employee;
1.SELECT count(*)
CROSS JOIN
VIEWS
2.FROM HumanResources.Department;
19
04-08-2023
Sub Query in 1.SELECT NationalIDNumber, JobTitle, HireDate Sub Query in 1.SELECT BusinessEntityID,NationalIDNumber, JobTitle, HireDate
6.) 6.)
7.ORDER BY JobTitle;
20
04-08-2023
To Find Third Highest Value (distinct) Find PurchaseOrderID, UnitPrice from PurchaseOrderDetail where
Unit Price is greater than Avg of List Price from Product Table
1.SELECT Min(UnitPrice)
Sub Query in 2.FROM Purchasing.PurchaseOrderDetail
Sub Query in The exists construct returns the value Sub Query in 1.SELECT BusinessEntityId, JobTitle
Where Clause true if the argument subquery is Where Clause 2.FROM HumanResources.Employee
– nonempty. – 3.WHERE EXISTS
Exists exists r r Ø Exists 4.(SELECT DepartmentID
5.FROM HumanResources.Department
6.WHERE Name = 'Sales'
7.);
21
04-08-2023
22
04-08-2023
23