What time data can a TIME format hold?
TIME can only hold time information
What data can DATE hold?
DATE can only hold date information, no time
What information can the TIMESTAMP datatype hold?
TIMESTAMP can hold both date and time information
What information can the TIMESTAMPTZ datatype hold?
TIMESTAMPTZ can contain date, time and timezone information
What does the EXTRACT() function do?
What does the AGE() function do?
What does the TO_CHAR() function do?
Lets you parse the datetime to a specific format. For example:
SELECT TO_CHAR(payment_date, ‘MM-YYYY’)
Documentation: https://www.postgresql.org/docs/12/functions-formatting.html
Can you use EXTRACT, AGE, TO_CHAR functions outside of SELECT?
Yes, they can be used to filter out results inside a WHERE clause for example
How can I concatenate strings inside a query?
SELECT first_name || ‘ ’ || last_name as full_name
FROM customer
Result:
Trevor Lahey
How can I uppercase strings inside a query?
SELECT upper(first_name)
What is a sub query?
Example:
SELECT * FROM table
WHERE something > (SELECT somethingElse FROM table)
Given the following table named test_scores:
student,grade
How can I find all students that are better than average using a sub query?
SELECT student,grade
FROM test_scores
WHERE grade > (SELECT AVG(grade) FROM test_scores)
What gets executed first? The main query or the sub query?
The sub query
Can you add other tables using sub queries?
Yes, just like JOIN, sub queries can access data from other tables.
Example:
SELECT student,grade
FROM test_scores
WHERE student IN (SELECT student FROM honor_roll_table)
What operator do I need to use if the sub query results in multiple values?
IN operator
Can a sub query substitute a JOIN?
Yes, JOIN commands such as INNER JOIN, LEFT OUTER JOIN etc will generally execute faster, however some people prefer sub queries because they are easier to read. Both are valid in most use cases
Postgresql will actually rewrite a query to a JOIN or sub query when it thinks it could lead to faster performance.
Can sub queries and JOIN statements be used in conjunction with each other?
Yes, example:
SELECT film.film_id,title
FROM film
WHERE film_id IN
(SELECT inventory.film_id
FROM rental
JOIN inventory ON inventory.inventory_id = rental.inventory_id
WHERE return_date BETWEEN ‘2005-05-29’ AND ‘2005-05-30’)
ORDER BY title
What does the EXISTS operator do?
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
Example:
SELECT first_name, last_name
FROM customer as c
WHERE EXISTS(
SELECT * FROM payment as p
WHERE p.customer_id = c.customer_id
AND amount > 11
)
What would the following query look like if it was written using INNER JOIN:
SELECT first_name, last_name
FROM customer as c
WHERE EXISTS(
SELECT * FROM payment as p
WHERE p.customer_id = c.customer_id
AND amount > 11
)
SELECT first_name,last_name
FROM customer
JOIN payment p on customer.customer_id = p.customer_id
WHERE p.customer_id = customer.customer_id AND amount > 11
What would the following query look like if it was written using INNER JOIN:
SELECT first_name, last_name
FROM customer as c
WHERE EXISTS(
SELECT * FROM payment as p
WHERE p.customer_id = c.customer_id
AND amount > 11
)
SELECT first_name,last_name
FROM customer
JOIN payment on customer.customer_id = p.customer_id
WHERE amount > 11
ORDER BY first_name
Advantages and Disadvantages of JOIN
Advantages Of Joins:
Disadvantages Of Joins:
Advantages and Disadvantages of Sub queries
Advantages Of Subquery:
Disadvantages of Subquery:
What is a self join?
The SQL SELF JOIN is used to join a table to itself as if the table were two tables; temporarily renaming at least one table in the SQL statement.
This is useful if I want to format the results of a table clearer.
For example given a table with employees and each employee also has a manager_id but each manager is also an employee with a employee_id, in which case I can show the managers name next to the employees name like so:
SELECT
employee. Id,
employee. FullName,
employee. ManagerId,
manager. FullName as ManagerName
FROM Employees employee
JOIN Employees manager
ON employee.ManagerId = manager.Id