SQL
Create database Q
use Q
Create table Sales(Item varchar(15),Price int, Qty int)
============================================================================
Alter table Sales add Zone varchar(15)
============================================================================
alter table Sales alter column Zone char(11)
============================================================================
Alter table Sales drop column Zone
============================================================================
Sp_rename 'Sales','Sale'
Sp_rename 'Sale','Sales'
============================================================================
sp_rename 'Sales.Qty', 'Quantity', 'COLUMN'
============================================================================
drop table Sales
============================================================================
Delete Sales
============================================================================
update Sales set Price = 500
update Sales set Price = 1100 where Item in('Apple','Grapes')
============================================================================
Drop table Sales
============================================================================
insert into Sales values('Apple',15,120)
insert into Sales values('Orange',25,20)
insert into Sales values('Papaya',35,520)
insert into Sales values('Grapes',45,320)
insert into Sales values('Pineapple',40,250)
============================================================================
insert into Sales(Item,Quantity) values('Pen',120)
insert into Sales(Price,Quantity) values(11,120)
============================================================================
Select * from Sales
Select Item,Quantity from Sales
============================================================================
Select Item, Price, Price * Quantity as TotalCost from Sales
============================================================================
Select * from Sales
Select Item,Price from Sales
Select Item, Price from (Select * from Sales) T where Price > 35
============================================================================
Select * from Sales where Item = 'Grapes'
Select * from Sales where price > 35
============================================================================
Select Item, sum(Price) as Total from Sales group by Item
Select Item, count(Price) as CountData from Sales
group by Item
Select Item, Avg(Price) as AvgData from Sales
group by Item
Select Item, Max(Price) as MaxData from Sales
group by Item
Select Item, Min(Price) as MinData from Sales
group by Item
Select Item,Zone, sum(Price) as Total from Sales
group by Item,Zone
============================================================================
Select Item,sum(Price) as Total from Sales group by Item having sum(Price) > 50
Select Zone,count(Qty) as c from Sales group by Zone having count(Qty) > 3
Select Zone,Sum(Qty) as s from Sales group by Zone having Sum(Qty) > 3
Select Zone,Avg(Qty) as a from Sales group by Zone having Avg(Qty) > 3
Select Zone,Min(Qty) as m from Sales group by Zone having Min(Qty) > 3
Select Zone,Max(Qty) as m from Sales group by Zone having Max(Qty) > 3
============================================================================
Select Item,Price from Sales order by Price Desc
Select Item,Price from Sales order by Item,Price Desc
Select Item,Price from Sales order by 1
Select * from Sales order by 4
============================================================================
Create table Sales (ID int,Item varchar(15), Price int, Quantity int)
insert into Sales values(10,'Apple',15,120)
insert into Sales values(11,'Orange',25,20)
insert into Sales values(12,'Papaya',35,520)
insert into Sales values(13,'Grapes',45,320)
insert into Sales values(14,'Pineapple',40,250)
insert into Sales values(18,'Banana',22,470)
Create table Location (ID int,Zone varchar(15))
Insert into Location values(10,'East')
Insert into Location values(11,'West')
Insert into Location values(12,'North')
Insert into Location values(13,'South')
Insert into Location values(15,'North East')
Insert into Location values(16,'South West')
============================================================================
Select ID from Sales where ID > 11 and ID < 14
Select ID,Price from Sales where Price > 25 and Price < 40
Select Quantity from Sales where Quantity > 40 and Quantity < 250
Select ID,Zone from Sales where ID > 11 and Zone = 'South'
Select * from Sales where
ID > 11 and
Item = 'Papaya' and
Price > 20 and
Quantity > 300 and
Zone = 'East'
Select ID,Zone from Sales where ID > 12 or zone = 'south'
Select * from Sales where
ID > 15 or
Item = 'Apple' or
price > 22 or
Quantity > 500 or
Zone = 'East'
Select ID,Item from Sales where item in('Apple','Papaya','Grapes')
Select ID,Item,Zone from Sales where item in('Apple','Papaya','Grapes') or
zone in('North','West')
Select ID,Item from Sales where item not in('Apple','Papaya','Grapes')
Select ID,Item,Price from Sales where ID between 12 and 17
Select ID,Item,Price from Sales where ID between 17 and 12
Select ID,Item,Price from Sales where ID not between 12 and 17
Select Item from Sales where Item like 'A%'
Select Item from Sales where Item like '%E'
Select Item from Sales where Item like '_A%'
Select Item from Sales where Item like '[A-G]%'
Select Item from Sales where Item like '[^A-G]%'
============================================================================
Create table S2021 (ID int,Item varchar(15), Price int,Zone varchar(15))
insert into S2020 values(11,'Orange',25)
insert into S2020 values(12,'Papaya',35)
insert into S2020 values(10,'Apple',15)
insert into S2021 values(13,'Grapes',45,'North')
insert into S2021 values(14,'Orange',40,'South')
insert into S2021 values(15,'Banana',22,'East' )
union
Select * from S2021
union
Select Price,Item from S2021
Select Price,Item from S2020
union
Select Price,Item from S2021
Select Price,Item from S2020
union
Select Price,Item from S2021
Select Item from S2020
union
Select Item from S2021
Select Item from S2020
union all
Select Item from S2021
Select Price,Item from S2020
intersect
Select Price,Item from S2021
Select Price,Item from S2020
Except
Select Price,Item from S2021
Select Price,Item from S2021
Except
Select Price,Item from S2020
============================================================================
Select Top 2 Price,Id,Zone from Sales
Select top 50 percent * from emp
============================================================================
Select * into Newtable from sales where 1 = 0
Select * into newt from Sales where 1 = 1
============================================================================
with CTE as ( Select Item,Price from Sales ) Select * from CTE where Item in('Grapes','Apple')
============================================================================
with CTE as( Select Item,Zone,Price,DENSE_RANK() over(order by price) as Rnk from Sales) Select * from CTE where rnk = 2
============================================================================
Select Item,Dense_Rank() over(Order by Price asc) as Dnsrnk, Rank() over(Order by Price asc) as rnk from Sales
============================================================================
Select Id,Item,Qty,Zone,max(Qty) over(partition by Zone) as ma from Sales
============================================================================
Create procedure SalesData
as
Select * from Sales where Zone = 'East'
Exec SalesData
============================================================================
Create Procedure Sales1
@@ZoneName Varchar(15)
as
Select * from Sales where Zone = @@ZoneName
Exec Sales1 'East'
Drop procedure Sales1
drop procedure SalesData
============================================================================
Create View [abcde] as Select * from Sales
Select * from abcde where item in('Apple','Grapes')
drop view abcde
============================================================================
Select GETDATE()
Select Year(getdate())
Select Month(getdate())
Select day(getdate())
Select datepart(YY,getdate())
Select datename(mm,getdate())
Select datename(DW,getdate())
Select datename(DD,getdate())
Select datename(DY,getdate())
Select datename(WW,getdate())
Select datename(QQ,getdate())
Select DATEADD(dd,11,getdate())
Select DATEDIFF(DD,GETDATE(),'20-April-2023')
============================================================================
Select Subject, Upper(Subject) as UpperCase from Marks
Select Subject, Lower(Subject) as LowerCase from Marks
Select Subject, Len(Subject) as LenOfmarks from Marks
Select Subject, left(Subject, 2) as LeftfunctionData from Marks
Select Subject, Right(Subject, 2) as RightFunctionData from Marks
Select Subject, reverse(Subject) as ReverseFunctionData from Marks
Select Subject, substring(subject,2,2) as PartOfString from Marks
Select Subject, charindex('En', subject) as PostionOfString from Marks
Select Subject, REPLACE(Subject, 's', 'sss') as ReplaceData from Marks
Select Subject, space(Left(Subject,3) + space(5) + right(Subject,2)) as AddSpace from Marks
Select Subject, Ltrim(subject) as RemoveSpaceFromLeft from Marks
Select Subject, Rtrim(subject) as RemoveSpaceFromRight from Marks
Select Subject, Replicate(Subject,3) as ReplicateSub from Marks
============================================================================
Select Subject,abs(Marks) as absoluteMarks from Marks
Select Subject,power(5,2) as PowerValue from Marks
Select Subject,marks,SQRT(Marks) as SQRTValue from Marks
Select Subject,marks,Square(Marks) as SquareValue from Marks
Select Subject,marks,round(SQRT(Marks),0) as RoundValue from Marks
Select Marks,CEILING(SQRT(Marks)) as NearesIncrementalValue from Marks
Select Marks,Floor(SQRT(Marks)) as NearesDecrementalValue from Marks
Select count(marks) from Marks
Select count(*) from Marks
Select COUNT_BIG(*) from Marks
============================================================================
Select Item,Price,Qty from Sales group by Item,Price,Qty
Select Item from Sales group by Item
Select Item,Qty from Sales group by Item,Qty
============================================================================
insert into Employee values(2,'mno',28000,20)
insert into Employee values(1,'abc',25000,20)
insert into Employee values(3,'pqr',32000,35)
insert into Employee values(4,'xyz',27000,25)
insert into Employee values(1,'abc',25000,20)
insert into Employee values(5,'zzz',40000,35)
SELECT id, name, Salary, COUNT(*) as duplicate_count
FROM employee
GROUP BY id, name, Salary
HAVING COUNT(*) > 1
SELECT id, name, Salary, COUNT(*) as duplicate_count
FROM employee
GROUP BY id, name, Salary
HAVING COUNT(*) = 1
============================================================================
Select ROW_NUMBER() over (order by zone) as rownumber from Sales
SELECT Price,Qty, ROW_NUMBER() OVER(Partition by Item
ORDER BY Item)
AS Row_Number from Sales
============================================================================
WITH cte AS (
SELECT
*,ROW_NUMBER() OVER(ORDER BY Item) row_num
FROM Sales)
SELECT Item,Price FROM cte
WHERE row_num >= 1 AND row_num <= 5;
============================================================================
Select Item, Price,Avg(Price) over(partition by Item) as AveragePrice from Sales
============================================================================
Select s.Id,s.Price,s.Zone,l.Locid,l.category from Sales s Left Outer Join Location L on s.Locid = L.Locid
============================================================================
Select s.Id,s.Price,s.Zone,l.Locid,l.category from Sales s Right Outer Join Location L on s.Locid = L.Locid
============================================================================
Select Item,Price from sales Order by Price Desc Offset 0 rows fetch next 5 rows only
============================================================================
Select CONCAT(ID, ' ' , Item), Price,
Case
when Price > 0 and Price < 500 then 'bad'
when Price > 501 and Price < 800 then 'good'
when Price > 801 then 'best'
end as Resultdata
from Sales Order by 1
============================================================================
Select * from sales where zone is null
Select * from sales where zone is not null