-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_Queries.sql
117 lines (88 loc) · 3.97 KB
/
SQL_Queries.sql
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- Select
Select * from Movies -- all movies
select MovieName as 'Film Adi',Description as 'Aciklama' from Movies --Only MovieNames and Descriptions
select FirstName+' '+LastName as 'FullName',UserName from Users
--Where
--Movies longer than 105 minutes
select * from Movies where Duration >105
--Movies between 2010 and 2016
-- Option I
select * from Movies where Year > 2010 and Year<2016 --(2010 and 2016 not included)
-- Option II
select * from Movies Where Year between 2010 and 2016 --(2010 and 2016 included)
--Null
select * from Movies where Rating is null
--Not Null
select * from Movies where Rating is not null
--Movies with a rating of 73 or 81
select * from Movies where Rating =73 or Rating = 81
select * from Movies Where Rating in (73,81)
--Order
select MovieName,Duration from Movies order by Duration asc -- ascending sort
select MovieName,Duration from Movies order by 2 asc --descending sort
--Like
select * from Movies where MovieName like 'A%' --Movies that start with 'A'
select * from Movies where MovieName like '%ad' --Movies that end with 'AD'
select * from Movies where MovieName like '__i%' --movies with 3rd letter 'i'
select * from Movies where Description like '%British%' --Movies that are british in the description
select * from Movies where MovieName Like '%[^r]' --Movies that don't end with r
select * from Movies where MovieName Like '%[SP]%' --You can find words with S or P in them.
--String Functions
select ASCII('E') -- ASCII CODE E=>101
select CHAR(101) -- letter (101 => e)
select CHARINDEX('@', '[email protected]') -- Location
select LEFT('Enes Serenli ', 4) --number of characters from the left
select Right('Enes Serenli', 4) --number of characters from the right
select Len('Enes Serenli') --number of character
select lower('ENES SERENLI') -- shrinks all characters
select upper('enes serenli') -- enlarges all characters
select LTRIM(' enes serenli') -- deletes spaces on the left
select RTRIM('enes serenli ') -- deletes spaces on the right
select LTRIM( RTRIM(' enes ' )) -- deletes spaces on the everywhere
select REPLACE('Enes&&Serenli','&','-') -- Replaces texts with new ones [(&) will replace it with (-) when it sees]
select SUBSTRING('enes serenli',2,8) -- Subtitle
select REPLICATE('Hellööö',5) --Repeats the specified text as many times as the value in the 2nd parameter
--Aggregate Functions
--Count
select count(*) as 'Film sayýsý' from Movies
--Sum
select sum(Duration) as 'Toplam Film Süresi' from Movies
--Max
select max(Rating) as [En yüksek Rating] from Movies
--Min
select min(Rating) as 'En düþük Rating' from Movies
--Avg
select avg(Duration) as 'Ortalama film süresi' from Movies
year() --function giving the year
getdate() -- func giving current date
year(getdate()) --Returns the year of the current date
--Group By
select DirectorId as 'Yönetmen ID',
count(MovieName) as 'Film Sayýsý'
from Movies
group by DirectorId
--SubQuery
select MovieName,
(select FullName from Directors d where d.Id=m.DirectorId) as 'Director Name' --parentheses is a subquery
from Movies m
select * from Comments c where UserId = (select Id from Users where FirstName = 'Enes') --parentheses is a subquery
--Having
select DirectorId,count(*) as 'Film Sayýsý'
from Movies
group by DirectorId
having count(*) >= 3 order by 2 asc
--Join => inner Join
Select u.FirstName+' '+u.LastName as 'FullName',
u.UserName,
c.Body,m.MovieName
from Users u
inner join Comments c on c.UserId=u.UserID
join Movies m on m.MovieID=c.MovieId
--Outer join
use Northwind
--Left outer Join
select calisan.FirstName as 'Çalýþan',mudur.FirstName as 'Müdür' from Employees calisan left outer join Employees mudur on calisan.ReportsTo=mudur.EmployeeID
--Right outer Join
select calisan.FirstName as 'Çalýþan',mudur.FirstName as 'Müdür' from Employees calisan right outer join Employees mudur on calisan.ReportsTo=mudur.EmployeeID
--cross join
select calisan.FirstName as 'Çalýþan',mudur.FirstName as 'Müdür' from Employees calisan cross join Employees mudur