-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComplex_SQL_Queries.sql
More file actions
101 lines (82 loc) · 5.23 KB
/
Complex_SQL_Queries.sql
File metadata and controls
101 lines (82 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
--Second Highest Value
--option 1
select top 1 SalesOrderDetailID from (
SELECT top 2 SalesOrderDetailID
FROM Sales.SalesOrderDetail
order by SalesOrderDetailID desc
) AS temp
order by SalesOrderDetailID
--option 2
select min(SalesOrderDetailID) from (
SELECT top 2 SalesOrderDetailID
FROM Sales.SalesOrderDetail
order by SalesOrderDetailID desc
) AS temp
--Finding all the duplicate records in a table.
--option 1
SELECT sod.SalesOrderID, COUNT(1) As duplicate_count
FROM Sales.SalesOrderDetail sod
GROUP BY SalesOrderID
HAVING COUNT(SalesOrderID) > 1
--Deleting all the duplicate recrods in a table.
;with CTE as
(
select Id,city,[state],
row_number() over (partition by City,[state] order by City) as CityNumber
from [CityMaster]
)
delete from CTE where CityNumber >1
--Query to fetch first record from a table
Select top 1 * from Sales.SalesOrderDetail
--Using partition & over caluse
select name, salary, gender
, count(gender) over (partition by gender) as gender
, avg(salary) over (partition by gender) as avgsal
, min(salary) over (partition by gender) as minsal
, max(salary) over (partition by gender) as maxsal
from employees
--Using rownumber() ... require over & order by caluses
select * From
(select LoginID, OrganizationLevel, gender
, ROW_NUMBER() over (partition by gender order by gender) as rownum
from HumanResources.Employee
) as t
where rownum < 4
-- print even & rows from empoyee table
select * From
(select LoginID, OrganizationLevel, gender
, ROW_NUMBER() over (partition by gender order by gender) as rownum
from HumanResources.Employee
) as t
--where (rownum%2) = 0 --even
where (rownum%2) = 1 --odd
-- create a table structure from another table
SELECT *
into #emp
FROM HumanResources.Employee
WHERE 1=2
-- select last 50% of the records from employee table
select * from (
select *,
ROW_NUMBER() over(order by BusinessEntityID) as rownum
from HumanResources.Employee
) as temp
where rownum > (select count(*)/2 from HumanResources.Employee)
-- select only common records from two tables
-- intersect does check all the columns where as inner join does only on defined columns.
select * from HumanResources.Employee
intersect
select * from HumanResources.Employee
-- year filter
select * from HumanResources.Employee
WHERE DATEPART(YEAR, HireDate) = 2007
--printing patterns
declare @i int =1
WHILE @i <4
BEGIN
print replicate('*',@i)
SET @i +=1
END
SELECT * FROM HumanResources.JobCandidate
--end