Please select at least PostgreSQL Server and pgAdmin for installation!
In case you still forget to select pgAdmin, you can always downloaded it separately here
Database Connection
When you open up pgAdmin, click on Servers, there should already be a default Server PostgreSQL <version>, with a postgres database in it. In case you don't, we can also create a new server and database with the following steps:
INSERT INTO users (email, username, password_hash) VALUES ('alice@example.com', 'alice', 'hash1');INSERT INTO users VALUES (DEFAULT, 'charlie@example.com', 'charlie', 'hash2');-- Do refer to the repo for complete code!
Update
UPDATE users SET username ='alex'WHERE username ='alice';
Delete
DELETEFROM users WHERE id =1;
Drop
DROPTABLE users;
Simple Queries
-- Retrieve all users:SELECT*FROM users;-- Retrieve a specific user by their email:SELECT*FROM users WHERE email ='bob@example.com';-- Retrieve all distinct usernames:SELECT DISTINCT username FROM users;-- Retrieve all users without a specific username:SELECT*FROM users WHERE username ISNULL;-- Retrieve all users whose username is not null:SELECT*FROM users WHERE username IS NOT NULL;
Algebraic Queries
Inner Join
-- Retrieve all notebooks along with their corresponding user information:SELECT nb.*, u.username, u.emailFROM notebooks nbINNER JOIN users u ON nb.created_by = u.id;-- is equivalent toSELECT nb.*, u.username, u.emailFROM notebooks nb, users uWHERE nb.created_by = u.id;
Left Outer Join
Here is an excellent article explaining the different join concepts, do give it a read!
OUTER JOINS are joins that return matched values and unmatched values from either or both tables. In the case of left outer join, we return matched values and unmatched values from the table to the left of the JOIN keyword.
-- Retrieve all notebooks along with their note ids, even if the notebook does not contain any note:SELECT nb.*, nt.id AS note_idFROM notebooks nbLEFT OUTER JOIN notes nt ON nb.id = nt.notebook_id;
Set Operators
UNION, INTERSECT, EXCEPT
They eliminate duplicates unless annotated with the keyword ALL.
To use the set operators on two tables, they must satisfy the condition:
The two queries must be union-compatible.
They must return the same number of columns with the same domains in the same order.
-- select customers who downloaded both version 1.0 and 2.0 for game AerifiedSELECT d.customerid FROM downloads dWHERE d.name ='Aerified'AND d.version ='1.0'INTERSECTSELECT d.customerid FROM downloads dWHERE d.name ='Aerified'AND d.version ='2.0';
Aggregate Queries
COUNT, MAX, MIN, AVG, SUM, STDDEV, ...
-- Retrieve the total number of users:SELECTCOUNT(*) FROM users;-- Retrieve total number of users w/ a username:SELECTCOUNT(username) FROM users;-- Retrieve total number of distinct usernames:SELECTCOUNT(DISTINCT username) FROM users;
Group By, Having
GROUP BY creates groups of records that have the same values for the specified fields before computing the aggregate functions. Without a clause, only one group is formed implicitly by SQL as soon as an aggregate function is called.
Aggregate functions cannot appear before GROUP BY(in WHERE), hence we have HAVING.
HAVING adds conditions to be checked after the evaluation of the GROUP BY clause. It only involves aggregate functions, columns listed in the GROUP BY clause and subqueries.
Attributes in the SELECT clause must appear in the GROUP BY clause, unless it is used in an aggregate function.
A slightly complicated query:
Try to figure out what it does!
SELECT u.username, COUNT(nb.id) AS total_notesFROM users uLEFT OUTER JOIN notebooks nb ON u.id = nb.created_byLEFT OUTER JOIN notes nt ON nb.id = nt.notebook_idGROUP BY u.id, u.usernameORDER BY total_notes DESC;
Nested Queries
Nest a subquery in a query.
Subqueries can be used only in FROM clause or WHERE clause
Comparison to a subquery should always use the quantifier ALL or ANY
Many ways to do the same thing. Just for illustration.
-- Retrieve user who created the most notesSELECT username, total_notesFROM (SELECT u.username, COUNT(nb.id) AS total_notesFROM users uLEFT OUTER JOIN notebooks nb ON u.id = nb.created_byLEFT OUTER JOIN notes nt ON nb.id = nt.notebook_idGROUP BY u.id, u.usernameORDER BY total_notes DESC) LIMIT1;