Select Statement
show the whole table Select * from <TableName> Select * from Sales.salesperson; Returns the total number of rows that match our search. Select count (*) From Person.Person; Top 10 Select top 10 * From Person.Address bottom 10 Select top 10 * From person.Address order by AddressID desc Select Statement
SUM Select sum(TotalDue) From Sales.SalesOrderHeader; AVERAGE select avg(TotalDue) from Sales.SalesOrderHeader; Largest Value select max(TotalDue) from Sales.SalesOrderHeader; Smallest Value select min(TotalDue) from Sales.SalesOrderHeader; Select Statement
Multiple values displayed select Count(TotalDue) as NumOfOrders ,min(TotalDue) as [Min], max(TotalDue) as [Max], sum(TotalDue) as [TOTAL], AVG(TotalDue) as Average from Sales.SalesOrderHeader Where PurchaseOrderNumber is not Null; DistinctDistinct Clause Select Distinct [Name], CurrencyCode from sales.ss_currency select * from sales.ss_currency Date and timeDate and time select GETDATE(); extract Date Select FORMAT(GETDATE(),'dd/MM/yyyy') extract time select FORMAT(GETDATE(),'hh:mm:ss') select * from sales.SpecialOffer where format(StartDate, 'dd/MM/yyyy') = '31/05/2011' order by DiscountPct desc |
set Database to be used
use AdventureWorks2019 Joins
INNER JOINS Select sod.productID as [Product], soh.SalesOrderNumber as [OrderNumber], soh.CustomerID as [Customer], SOH.TotalDue as [TOTAL] from sales.SalesOrderDetail SOD inner join Sales.SalesOrderHeader SOH on SOD.SalesOrderID = SOH.SalesOrderID; Joins
LEFT Join Select PP.BusinessEntityID, PP.FirstName as [FirstName], PP.LastName as [Surname], PPH.PhoneNumber as [PhoneNumber] from Person.Person PP left join Person.PersonPhone PPH on PP.BusinessEntityID = PPH.BusinessEntityID where PPH.BusinessEntityID is NULL; Joins
LEFT Outer Join Select PP.BusinessEntityID, PP.FirstName as [FirstName], PP.LastName as [Surname], PPH.PhoneNumber as [PhoneNumber] from Person.Person PP left outer join Person.PersonPhone PPH on PP.BusinessEntityID = PPH.BusinessEntityID order by pp.BusinessEntityID Having Clauseselect CurrencyCode, Name from sales.ss_currency group by CurrencyCode, Name having count (*)>1 Acending / DecendingOrder BY Decending Select * From Sales.SalesReason Order by [name] desc; Order BY Acending Select * From Sales.SalesReason Order By [name]; Update
Update multiple rows Update sales.SS_Currency Set CurrencyCode = 'SHI' , [Name] = 'Shi Dollar' Where CurrencyCode = 'SHD'; select * from sales.SS_Currency Where CurrencyCode = 'SHI' or currencyCode ='BKS'; UpdateUpdate multiple columns select * from sales.SS_Currency Update sales.SS_Currency Set Name = 'Shilpa Dollar' Where CurrencyCode = 'SHD' or currencyCode ='BKS'; select * from sales.SS_Currency Where CurrencyCode = 'SHD' or currencyCode ='BKS'; |
Change Tables
Tables
Create Table Create Table sales.SS_salesperson_Own (SalesPersonID int not NULL Identity, SalesPersonName varchar(100) not Null, ModifiedDate datetime, Notes varchar(max), IsManager bit, commission decimal(10,2)) Changing Data
UPDATE update sales.SS_Currency set [Name] = 'SS dollar', ModifiedDate = GETDATE() where CurrencyCode = 'SSD'; select * from sales.SS_Currency where CurrencyCode = 'SSD'; DELETE Delete from sales.SS_Currency where CurrencyCode = 'SSD'; select * from sales.SS_Currency where CurrencyCode = 'SSD'; INSERT INTO Insert Into sales.SS_Currency (CurrencyCode, [Name], ModifiedDate) Values ('SSD','ShilpaDollar', GETDATE()) Insert
insert into sales.SS_salesperson_Own ( SalesPersonName, ModifiedDate , Notes , IsManager, commission) values ( 'James Smith', GETDATE(), 'likes to sing', 0, 456.55), ( 'carrol james', GETDATE(), 'likes to dance', 0, 556.55), ( 'Aarav Prasad', GETDATE(), 'likes to play guitar', 0, 1000.00), ( 'Aanya Prasad', GETDATE(), 'likes to play Cello', 0, 1000), ( 'KPBS', GETDATE(), 'likes chips', 0, 5943.24), ( 'SS', GETDATE(), 'likes to play Cello', 0, 1000000) |
Cheatography
https://cheatography.com
SQL Server Recipies Cheat Sheet (DRAFT) by freesprited
SQL Server Recipies - for update, delete, join etc
This is a draft cheat sheet. It is a work in progress and is not finished yet.