-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04. Basic CRUD.sql
86 lines (70 loc) · 1.53 KB
/
04. Basic CRUD.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
USE SoftUni
--String concatenation
SELECT
FirstName + ' ' + LastName AS 'Full Name',
JobTitle,
Salary
FROM Employees
--Distinct
SELECT DISTINCT JobTitle DepartmentID
FROM Employees
SELECT
DISTINCT
JobTitle
FROM Employees
WHERE DepartmentID = 1
--Different - Razlichno
SELECT
LastName
FROM Employees
WHERE ManagerID <> 3 and ManagerID <> 4
--BETWEEN
SELECT
LastName,
Salary
FROM Employees
WHERE Salary BETWEEN 20000 AND 22000
--IN/NOT IN
SELECT
FirstName,
LastName,
ManagerID
FROM Employees
WHERE ManagerID IN (109, 3 ,16) --Some of this values
--CHECK FOR NULL
SELECT
LastName,
ManagerID
FROM Employees
WHERE ManagerID IS NULL -- IS NOT NULL, but = null not work
--ORDER BY
SELECT
LastName,
HireDate
FROM Employees
ORDER BY HireDate DESC
USE [Geography]
--Select top rows
SELECT
TOP(2) *
FROM Peaks
ORDER BY Elevation DESC
--CASE WHAN
--P10 with check for null in columns
SELECT
CASE WHEN FirstName IS NULL THEN '' ELSE FirstName END +
CASE WHEN MiddleName IS NULL THEN '' ELSE ' ' + MiddleName END +
CASE WHEN LastName IS NULL THEN '' ELSE ' ' + LastName END
FROM Employees
WHERE Salary IN (25000, 14000, 12500, 23600)
--UPDATE AND GETDATE
UPDATE Projects
SET EndDate = GETDATE()
WHERE EndData IS NULL
UPDATE Projects
SET EndDate = '2017-01-23'
WHERE EndDate IS NULL
--ISO DATE FORMATE - SMALLDATETIME is minute precision, must be DATETIME OR DATETIME2
UPDATE Projects
SET EndDate = '2019-01-16T10:23:32.989'
WHERE EndDate IS NULL