04 Advanced SQL Commands
04 Advanced SQL Commands
04 Advanced SQL Commands
Topics
SQL
● Section Overview
○ Timestamps and EXTRACT
○ Math Functions
○ String Functions
○ Sub-query
○ Self-Join
Timestamps and Extract
PART ONE
DISPLAYING CURRENT TIME INFORMATION
SQL
● EXTRACT()
○ Allows you to “extract” or obtain a sub-component
of a date value
■ YEAR
■ MONTH
■ DAY
■ WEEK
■ QUARTER
SQL
● EXTRACT()
○ Allows you to “extract” or obtain a sub-component
of a date value
■ EXTRACT(YEAR FROM date_col)
SQL
● AGE()
○ Calculates and returns the current age given a
timestamp
○ Useage:
■ AGE(date_col)
○ Returns
■ 13 years 1 mon 5 days 01:34:13.003423
SQL
● TO_CHAR()
○ General function to convert data types to text
○ Useful for timestamp formatting
○ Usage
■ TO_CHAR(date_col, ‘mm-dd-yyyy’)
SQL
● Expected Result
SQL
● Hints
○ You do not need to use EXTRACT for this query.
SQL
● Solution
● SELECT
DISTINCT(TO_CHAR(payment_date,'MONTH'))
FROM payment
SQL
● Expected Result
○ 2948
SQL
● Hints
○ Use EXTRACT
○ Review the dow keyword
○ PostgreSQL considers Sunday the start of a week
(indexed at 0)
SQL
● Solution
● SELECT COUNT(*)
FROM payment
WHERE EXTRACT(dow FROM payment_date) = 1
Mathematical Functions
SQL
● Standard Query
○ SELECT student,grade
FROM test_scores
SQL
● Typical Syntax
SELECT column_name
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name
WHERE condition);
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
1 Andrew 3
2 Bob 3
3 Charlie 4
4 David 1
SQL
EMPLOYEES
1 Andrew 3
2 Bob 3
3 Charlie 4
4 David 1
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM table AS tableA
JOIN table AS tableB ON
tableA.some_col = tableB.other_col
EMPLOYEES
1 Andrew 3
● Syntax
○ SELECT tableA.col, tableB.col
FROM employees AS tableA
JOIN employees AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM employees AS tableA
JOIN employees AS tableB ON
tableA.some_col = tableB.other_col
SQL
● Syntax
○ SELECT tableA.col, tableB.col
FROM employees AS tableA
JOIN employees AS tableB ON
tableA.some_col = tableB.other_col
EMPLOYEES
1 Andrew 3
● Syntax
○ SELECT emp.col, tableB.col
FROM employees AS emp
JOIN employees AS tableB ON
emp.some_col = tableB.other_col
SQL
● Syntax
○ SELECT emp.col, tableB.col
FROM employees AS emp
JOIN employees AS tableB ON
emp.some_col = tableB.other_col
SQL
● Syntax
○ SELECT emp.col, tableB.col
FROM employees AS emp
JOIN employees AS tableB ON
emp.some_col = tableB.other_col
EMPLOYEES
1 Andrew 3
● Syntax
○ SELECT emp.col, report.col
FROM employees AS emp
JOIN employees AS report ON
emp.some_col = report.other_col
SQL
● Syntax
○ SELECT emp.col, report.col
FROM employees AS emp
JOIN employees AS report ON
emp.some_col = report.other_col
SQL
● Syntax
○ SELECT emp.col, report.col
FROM employees AS emp
JOIN employees AS report ON
emp.emp_id = report.report_id
SQL
● Syntax
○ SELECT emp.col, report.col
FROM employees AS emp
JOIN employees AS report ON
emp.emp_id = report.report_id
SQL
● Syntax
○ SELECT emp.name, report.name
FROM employees AS emp
JOIN employees AS report ON
emp.emp_id = report.report_id
SQL
● Syntax
○ SELECT emp.name, report.name
FROM employees AS emp
JOIN employees AS report ON
emp.emp_id = report.report_id
SQL
● Syntax
○ SELECT emp.name, report.name AS rep
FROM employees AS emp
JOIN employees AS report ON
emp.emp_id = report.report_id
SQL
Andrew Charlie
Bob Charlie
Charlie David
David Andrew
SQL