Repository navigation
Expand file tree
/
Copy path05_SQL_Where-conditions.sql
More file actions
119 lines (85 loc) · 2.81 KB
/
Copy path05_SQL_Where-conditions.sql
File metadata and controls
119 lines (85 loc) · 2.81 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-----------------WHERE OPERATORS--------------------
/*
-comparison operators
-logical operators
-range operator
-membership operator
-search operator
*/
/*-------comparison operators---------*/
/*
structure:
condition -> EXPRESSION Operator EXPRESSION
*/
/*task: Retrieve all customers from Germany*/
Use MyDatabase
SELECT*
FROM dbo.customers
WHERE country = UPPER('GERMANY') -- porownuje ze stringem w tabeli ale zapisanym wielkimi literami
/*task: Retrieve all customers who are not from Germany*/
SELECT*
FROM dbo.customers
WHERE country != UPPER('GERMANY') --- mozna zamiast != uzyc <> to dziala tak samo
/*task: Retrieve all customers with a score greater than 500*/
SELECT*
FROM dbo.customers
WHERE score > 500
/*task: Retrieve all customers with a score 500 or more*/
SELECT*
FROM dbo.customers
WHERE score >= 500
/*task: Retrieve all customers who are from the USA and have a score greatere than 500*/
SELECT*
FROM dbo.customers
WHERE country = UPPER('USA') AND score > 500 -- to i to ma byc prawdziwe
/*task: Retrieve all customers who are either from the USA or have a score greater than 500*/
SELECT*
FROM dbo.customers
WHERE country = UPPER('USA') OR score > 500 -- to albo to ma byc prawdziwe
/* Retrieve all customers with a score NOT less than 500*/
SELECT*
FROM dbo.customers
WHERE NOT score < 500 -- not odwraca logike zapytania
/* Retrieve all customers whose score falls in the range between 100 and 500*/
SELECT*
FROM dbo.customers
WHERE score BETWEEN 100 AND 500 -- between to jest nasz zakres i jest on inkluzywny (100 i 500) tez naleza
/*task: Retrieve all customes from either Germany OR USA*/
SELECT*
FROM dbo.customers
WHERE country IN (UPPER('Germany'), UPPER('Usa')) -- sprawdza czy cos jest "IN" lista. liste wypisuje w nawiasie
-- jest tez polecenie NOT IN ktore dziala analogicznie (jest to polaczenie bardziej ale zostawmy to tak)
/*-----------LIKE----------*/
/*
schemat: like -> pattern -> % lub _
% - oznacza ze cokolwiek spelnia warunki naszego zapytania np po jakiejs literze
_ (underscore) - oznacza ze tak jakby exact one (patrz przyklad na dole), poprostu cos ma byc w tym miejscu
ex.1
M%: Maria, Ma, M - ok
Emma - zle
ex.2
%in: Martin,Gin,in - ok
intern - zle
ex.3
%r% - znaczy ze jesli gdziekolwiek wystapi 'r' to spelni warunek
ex.4
__b%: Albert,abbek,Rob - ok
Abel, An - zle
*/
/*task: Find all customers whose first name starts with 'M'*/
SELECT*
FROM dbo.customers
WHERE first_name LIKE 'M%'
/*task: Find all customers whose first name contains 'r'*/
SELECT*
FROM dbo.customers
WHERE first_name LIKE '%r%'
/*task: Find all customersa whose first name ends with 'n'*/
SELECT*
FROM dbo.customers
WHERE first_name LIKE '%n'
/*task: Find all customers whose first name has 'r' in the third position*/
SELECT*
FROM dbo.customers
WHERE first_name LIKE '__r%'
--extra logical operators: ALL, EXISTS,