-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_lesson.sql
More file actions
56 lines (47 loc) · 844 Bytes
/
functions_lesson.sql
File metadata and controls
56 lines (47 loc) · 844 Bytes
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
SELECT
first_name,
SUBSTR(first_name, 3, 3),
SUBSTR(first_name, 3, 1)
FROM employees
LIMIT 10;
SELECT
first_name,
LEFT(first_name, 3),
RIGHT(first_name, 3),
LEFT(RIGHT(first_name, 3), 2),
LEFT(RIGHT(CONCAT(first_name, ' ', last_name), 3), 4)
FROM employees
LIMIT 10;
SELECT
first_name,
LEFT(first_name, 100)
FROM employees
LIMIT 10;
SELECT
*,
REPLACE(dept_no, 'd00', 'Dept No.')
FROM departments;
# We're in UTC on the server
SELECT
NOW(),
CURDATE(),
CURTIME();
# how many days old is each employee?
SELECT
birth_date,
DATEDIFF(NOW(), birth_date) / 365.25 AS years_old
FROM employees
LIMIT 50;
SELECT
MIN(emp_no),
MAX(emp_no),
AVG(emp_no)
FROM employees
LIMIT 20;
SELECT
NOW() - birth_date
FROM employees;
# average employee age
SELECT
AVG(DATEDIFF(NOW(), birth_date) / 365.25) AS average_age
FROM employees;