SQL Guide
SQL Guide
SQL Queries 1
Getting started:
Go ahead and launch PGadmin4 and click on a small icon labeled servers in the upper left
corner. This icon should expand and reveal another icon called PostgreSQL 10 .
Make sure you remembered you password from when you set up PGSQL, otherwise you
will have to go though the entire install process and choose another password in order to
get back to this point.
SQL Queries 2
If PGadmin4 doesn’t accept the password, you can try to leave the field blank, but
chances are you’ll most likely need to re-install everything.
Once the connection has been made, you should be able to expand the PGSQL icon to
reveal databases
Then give you database a name. For the dvdrental toy database, we’ll just give it a name like
dvdrental .
Now you should be able to see your new database under databases .
The restore menu will pop up and give you some options of how to load the content
inside of your database.
This option will allow for you to find and select the .tar file for it to be uploaded in the
restore.
Doing so will load your fresh database with all of the data contained inside of the .tar file.
Once the process starts, you might get some buggy behavior.
Once the restore process is completed, you can right click on your database, then select
refresh.
Then you can right click on the database again and then select query tool .
Now that your query tool has been retrieved you can begin running your queries.
Then a form modal will pop up allowing you to name your database.
Using a SQL Query in the console You can easily create a new database in the console
using SQL by right-clicking on postgres and then selecting query tool .
Once your query tool modal pops up, you can user this query to create a new database:
SQL Queries 3
CREATE DATABASE <database name>;
Using a SQL Query in the console You can easily create a new database in the console
using SQL by right-clicking on postgres and then selecting query tool .
Once your query tool modal pops up, you can user this query to create a new database:
Restore a Database:
To accomplish this, all you need to do is right click a freshly created database and then select
restore. Then the restore menu will pop up so you can select what data to restore the database
to.
There are two ways you can restore a database with only the table schemas:
Option 1:
Then right click the newly created database and select restore .
The restore modal will pop up and then you can select a .tar file containing a saved
database.
PGadmin4 will then load your new database with only the schemas and not the data
contained inside of them.
Option 2:
Then right click the newly created database and select restore .
The restore modal will pop up and then you can select a .tar file containing a saved
database.
Go to the restore options tab and then select Only Schema , and then Clean before
restore .
PGadmin4 will then load your new database with only the schemas and not the data
contained inside of them.
SQL Queries
SQL Queries 4
The following syntax covered in this section can be used for any SQL Platform.
One important note about using the * inside of select statement to query a small set of data.
This isn’t the most efficient way to query a SQL database because it will increase traffic to your
SQL server, thus slowing it down.
However for the purpose of this exercise, you can use it for practice and familiarity since the dvd
Exercise Time!
You’re an employee at ABC DVD Rentals and your manager has told you they’re planning on
sending out promotional coupon emails to your existing customers!
Write a query that selects the first name, last name, and email from customer.
3 Minutes!
SQL Queries 5
Here are some really good examples for this:
Exercise Time!
It’s another day at the DVD store, and a new customer is wanting to know all of the distinct
rating types that are available in the store.
Write a query that returns the distinct rating values for all the films in the database.
HINT: You’ll need to use the film table inside of the dvdrental database.
3 Minutes!
Operator Description
= Equal
SQL Queries 6
> Greater Than
OR Logical Operator OR
You can also combine this query with a logical operator OR if you also wanted to know if they
paid $4.99 or $1.99.
Exercise Time!
Time to practice your SELECT WHERE statement.
Challenge #1 :trophy:
It’s another day at the DVD rental store and someone brings a wallet they found on the floor to
the front desk where you’re working at. The wallet belongs to
Nancy Thomas. Using all of the knowledge we have now, try to think of a way to query Nancy’s
email from the database.
5 minutes!
SQL Queries 7
5 minutes
Your manager wants you to look up the first person on the list because they are 30 days late.
Their address is ‘259 Ipoh Drive’, using what we know about SELECT WHERE , query your database
for the phone number for this person.
5 minutes
You can also pass a specific column name as an argument into the count parameters, but since
count actually returns the number of rows, you can just pass in an asterisk instead.
We can also pass in the SQL keyword, DISTINCT to get the count of distinct rows inside of a
column like this:
So, in essence, the count function will return the number of rows returned by a SELECT clause.
SELECT COUNT(phone)
FROM address
WHERE address = '259 Ipoh Drive'--> 1
So, if we used this clause for that last query you ran to find the phone number, this would return
1, since there is only one row containing a phone number matching the where condition.
SQL Queries 8
SELECT *FROM customer
LIMIT 1;
-- This will return the first row in the database
ORDER BY
This clause allows us to sort how data is returned either by ascending or descending order
based on a specific criteria.
NOTE The ORDER BY clause will always result in ASC ascending order by default if none specified.
So, for example, you could order customer in the dvdrental database like so:
SELECT last_name
FROM customer
ORDER BY last_name DESC-- This query will essentially return all of the
names from the last names column,-- but sorted by the first name in desc
ending order.
Try this last query inside of the dvdrental toy database, and look for how the two people with the
first name Kelly are sorted (ROWS 327-328).
Exercise Time!
Challenge #1 :trophy:
It’s another day at the DVD rental store and your manager has decided to do something extra
special for your top customers.
That said, your manager wants you to query the database to return the customer ID numbers for
the top ten highest payment amounts.
1st HINT You’ll need to look around for the right table to run your query on.
SQL Queries 9
2nd HINT You’ll need to take your prior knowledge to limit the amount of rows returned in your
query.
5 minutes
Go ahead and query your database engine to return the titles for movies with a film id from 1-5.
1st HINT You’ll need need to look around for the right table.
2st HINT You can use a LIMIT or WHERE clause for this query.
This statement provides us with a nice way of writing a query that basically accomplishes the
following:
So, in essence, we could use our dvdrental database to query specific data using a between
statement like this:
SELECT amount
FROM payment
WHERE amount
BETWEEN .99 AND 2.99;
-- This query would return all values greater than or equal to .99-- and
less than or equal to 2.99
You can also use NOT BETWEEN to return values that are not in between the given range:
SELECT amount
FROM payment
WHERE amount
NOT BETWEEN .99 AND 2.99;
We could technically get away with using comparison operators, but the BETWEEN statement just
makes our queries much more elegant.
SQL Queries 10
Here’s another example of searching for a dates in a particular range using the BETWEEN
statement:
SELECT payment_date
FROM payment
WHERE payment_date
BETWEEN '2007-02-07'AND '2007-02-15'LIMIT 5;
You can literally use a string literal with the date inside using the format above.
The IN statement
This statement is always combined with the WHERE statement allows us to check if a values
matches any value in a list of values.
Let’s suppose you wanted to find where or not a person exists in your database. You know their
last name starts with Ka, but you’re not sure how it ends. You can use the LIKE % pattern
matching operator to find them. The LIKE statement always goes hand in hand with the WHERE
statement.
You can also use the % wildcard operator to find results matching all of the characters before a
known value. Let’s suppose you wanted to find all of the first names that end in the character y .
You could search like this.
SQL Queries 11
WHERE first_name LIKE '%y'ORDER BY first_name;
-- This query would return all of the names from the first_name column--
that end with the character y
There’s another way of using this wildcard operator where you can search for characters in
between a set of characters like this:
There’s another wildcard operator we can use, the _ , and this operator allows us to indicate a
single character.
So, in essence, we could find a name that contains certain characters that come immediately
after a single character.
Say we wanted to find a name containing ‘er’ that can directly after a single character.
You can also use the NOT statement directly before LIKE to get all pf the results that don’t match
the conditions:
5 Minutes
SQL Queries 12
You’re helping one of your co-workers organize the movies and you need to find all of the
movies that start with the letter -
5 Minutes
PART II
Now retrieve the names for those distinct districts.
right?
Count how many films have the word Truman somewhere in the title.
Challenge #6 :trophy: :trophy: :trophy: :trophy: :trophy: :trophy:
Your manager comes to you very concerned because they are curious as to how many
customers received a free rental from the employee with the staff_id of 1
Challenge #7 :trophy: :trophy: :trophy: :trophy: :trophy: :trophy: :trophy:
Now that you’ve discovered how many freebies were given, your manager now wants to know
the total amount pf payments that were charged by the employee with a staff_id of 1.
SELECT AVG(amount)
FROM payment;
-- We would get a huge number like this: "4.2006056453822965"
SQL Queries 13
Sometimes, we’ll end up with a number containing a large amount of decimal places.
To handle this, we can also use the ROUND() function to round our AVG operator to a specific
number of decimal places.
Here’s an example of that:
SELECT ROUND(AVG(amount), 2)
FROM payment;
MIN
This query allows us to select a row with the MIN min value. So, an example of that would be:
SELECT MIN(amount)
FROM payment;
SELECT ROUND(MIN(amount), 1)
FROM payment;
MAX
Max is very straightforward as it’s literally the exact opposite to MIN
Here’s an example:
SELECT MAX(amount)
FROM payment;
SELECT ROUND(MAX(amount), 2)
FROM payment;
So, for example, if we wanted to see all of the customer ids inside of the payment table:
In this instance, we’ve used our GROUP BY clause like a DISTINCT Statement
We could have easily performed this query like this and it would have worked:
SQL Queries 14
SELECT DISTINCT(customer_id) AS customers
FROM payment
ORDER BY customer_id ASCLIMIT 10;
Exercise Time!
Time to test our understanding of GROUP BY
Challenge #1 :trophy:
So, it’s another day in at work, you were recently promoted to assistant manager and your
branch manager has requested your help in determining how to reward the employee.
You both decided to reward the employee the highest transactions and sales tendered amount.
You’ll need to write a query that can help you determine how many transactions were performed
and the total of those transactions by customer
SQL Queries 15
In other words, the HAVING clause can be used to return rows from a GROUP BY statement
where a specific condition is true.
Another helpful way of thinking about the HAVING clause is that it’s almost like the WHERE clause,
but with a GROUP BY statement.
When and where would I use the HAVING clause versus the
WHERE statement?
The having clause sets the condition for the group rows created by the GROUP BY clause after
the GROUP BY clause applies.
The WHERE clause sets the the condition before the GROUP BY clause applies.
Exercise Time!
Now it’s time to practice with some of the skills we’ve learned so far.
Challenge #1 :trophy:
There’s exciting news at the DVD rental store, corporate headquarters has decided to debut a
new credit card program for customers who frequently visit the store.
Write a query that will allow you to find customers who have at least 40 total transaction
payments. You will need to use a GROUP BY and a HAVING statement for your query.
The AS statement
This statement allows us to rename columns to an alias of our choosing. This is especially
helpful when using JOINS
SQL Queries 16
Here’s a quick example of how this works:
An Overview of JOINS
Joins are extremely important to know for managing you database. Especially when it involves
relational database management systems (RDBMS) like PostgreSQL.
Suppose you have a table of customers and some kind of reference to your customers in
another table of addresses where each customer lives.
When the time comes when you need to find the address of each customer, how could your do
that?
The INNER JOIN , which can also be referred to as just a JOIN statement is just one example of a
statement that only returns matching corresponding rows between different tables.
Here’s some more examples:
We can shorthand our INNER JOIN queries also by just using JOIN and it will run as an INNER JOIN
by default.
SQL Queries 17
GROUP BY first_name, last_name
ORDER BY SUM(amount) DESC;
Again, INNER join will only connect the tables where they have matching corresponding rows.
Let’s suppose we had our films table that listed all of the films, our inventory table, and we
needed to know which films were not in inventory, we could use a LEFT JOIN to find the diff
based on the null values that will appear in the right side.
SELECT title,
film.film_id AS film_ids_from_films,
inventory.film_id as film_ids_from_inventory
FROM film
LEFT OUTER JOIN inventory
ON inventory.film_id = film.film_id
ORDER BY inventory.inventory_id DESC;
SQL Queries 18
With a left join, we we’ll get everything we requested with our FROM statement.
2 Shawshank Redemption 2 24
4 IP Man 4 321
UNION
Time to look at the UNION operator is another powerful SQL query that allows us to combine the
result sets of two or more SELECT statements into a single result set.
Although this is indeed a powerful SQL Query, it’s important to take note that there are some
important rules that come with it.
When combining two SELECT statements, both queries must return the same number of
columns.
So for example, if the first SELECT statement returns two columns, the second SELECT
Also, those corresponding columns in the queries must have compatible datatypes.
So, for example, if the first SELECT statement returns a column 1 of integer type data, then
the second SELECT statement for column 1 should return a column of integers.
SELF JOIN
This is a SQL query that allow you to join two rows on the same table. In order to make a self-
join, you’ll need to use an alias so your SQL engine will be able to tell both sides of your query
apart.
Let’s see an example:
SQL Queries 19
Say you wanted to see who in the customer table had the same name as first name as someone
else in the table who had the name as a last name.
SELECT a.customer_id,
a.first_name, a.last_name,
b.customer_id,
b.first_name, b.last_name
FROM customer AS a, customer AS b
WHERE a.first_name = b.last_name
One thing you’ll probably notice here is that for a SELF JOIN , this SQL syntax doesn’t use the JOIN
Now you could use the JOIN keyword if you like, but you’ll just need to remember to use your
aliases.
Although you could probably accomplish the same task with a subquery, there’s a huge
performance benefit to using a self-join.
An INNER JOIN or JOIN would work best for when you only want to return the matching results
from each table.
A ‘FULL OUTER JOIN’ would work best if you want to return ALL of the results of both
tables, regardless if either side has matching corresponding sides. So this query could
produce a result where two joined tables will have null values when there isn’t a match.
A LEFT OUTER JOIN or LEFT JOIN would return a complete data set for the left side, or in other
words, the table your including in your FROM statement / clause. Then the table your
including in your JOIN statement / clause , or the right side, would show all matching values,
and non-matching (null) values.
A RIGHT OUTER JOIN or RIGHT JOIN is nearly identical to the LEFT JOIN except it’s the complete
opposite. In this case, the table on the left side, or the table you’re including in your FROM
statement / clause would return all matching values and all non-matching (null) values to the
table included in your JOIN statement / clause, or the right side. This table would just show
all values.
You could technically create the same affect of a RIGHT JOIN using a LEFT JOIN' by switching the
SQL Queries 20
There are no specific use cases other than the business case and data organization / formatting
requirements you’ll need. Using Outer Joins are extremely useful when wanting to understand
where each data set differs, and perhaps displaying those differences in a meaningful way by
having the option of switching from the FULL , LEFT or RIGHT can make this process a lot easier.
SQL Timestamps
This section will go over timestamps and how to use them specifically while using PostgreSQL.
It’s important to know that each SQL engine uses timestamps differently, so it’s important to
refer to the documentation when dealing with timestamps with other SQL Engines.
SQL allows us to use the timestamp data type to retain time information. It’s also important to
know how to extract certain information from timestamp objects.
PostgreSQL as well as other SQL engines provide us with an extract function we can use to
extract information from timestamp objects.
customer_id day
341 15
343 16
261 16
sum month
28559.46 4
There are many things you can do with the extract function, but this is just a small example. If
you would like to see other tools you can use with this function besides extracting the month or
a day, you can read the official documentation
SQL Queries 21
Here’s a link to the documentation for Mathematical functions and operators.
There are many different SQL Mathematical functions we can use while working with a
database, but it’s only necessary to interact with the most common one:
The ROUND() SQL Mathematical function is and will be the most common one we’ll use.
Here’s an example of using some common mathematical operators in our queries.
2007-02-15
17503 341 2 1520 7.99
22:25:46.996577
new_id
343
Again, there are many things you can do with mathematical functions and operators, but this is
just a very high level overview of them.
String Functions
Here is the official documentation on string functions for PostgreSQL
PostgreSQL gives us a bunch of amazing string functions you can use to manipulate or analyze
columns containing data made of strings, such as text for example:
```SQL
SELECT first_name || ' ' || last_Name AS full_name
FROM customer
LIMIT 1;
SQL Queries 22
Jared Ely
first_name char_length
Jared 5
fname_allcaps
JARED
The Subquery
Here’s another powerful query we can use for SQL that can make data analysis so much easier.
Let’s say you wanted to know which films had a rental rate that was higher than the average
rental rate. One approach would be to find the average first, take note of it, and then run another
query to find the films where the rental rate was higher than the previous query’s result.
For for example:
SELECT AVG(rental_rate)
FROM film;
-- 3
Then you’d need to run another query just to find the diff where films are renting for a rate
higher than the average.
Here’s how we can address the same task, but with a SQL subquery inside of a WHERE clause:
Keep in mind that the subquery inside the parentheses runs first and then the outer query runs
second.
SQL Queries 23
SELECT title, rental_rate
FROM film
WHERE rental_rate<(SELECT AVG(rental_rate) FROM film)
This query would produce a result of all the films with a rental rate higher than the average we
found earlier.
Sub Queries can get extremely complex at times as they are a very powerful way to query your
database.
Here’s how they can become even more daunting:
Boolean
You must use the key work boolean or bool when declaring a new column with this data
type.
yes no
true false
1 0
y n
Character
Number
Integers.
SQL Queries 24
serial same as integer but PostgreSQL will auto populate this and it is auto.
incrementing.
float(n).
numeric or numeric(p,s) is a real number with p digits with an s number after the decimal
point. The numeric(p,) is the exact number.
date
time
timestamp
interval
timestamptz
Special Types
Array
So for example, for a table for customers having a customer id, name, phone number
etc. the customer id would most likely be the primary key, because it identifies that
customer row uniquely in the row in the table.
We would never want to use a first_name or last_name as a table’s primary key column
because there will be a possibility of having duplicate values, and that can cause a lot of
problems in the future.
A serial Number data type is typically used for the primary key, because it will auto-
increment itself as a new row is created - this will also help you avoid duplicate values.
So, where we’re creating a table and a primary key for that table, this is what it woild look like:
SQL Queries 25
Foreign Key
A Foreign key is a field or group of fields in a table that uniquely identifies a row in another
table. So in other words, a foreign key is defined in a table where it refers to the primary key
in another table.
So, the table that contains the foreign key is called the referencing table or child table, and the
table to which the foreign key references is called the referenced table, or parent table. A table
can have multiple foreign keys depending on its relationship with other tables.
Here’s an example. Let’s say we have the customer table and a payment table. The payment
table has a payment_id column, which represents each instance of a payment. The payment
table also has a foreign key called customer_id, which happens to be the primary key on the
customer table. So there might be multiple instances of payment columns on the payments table
with referencing the same customer_id from the customer table.
In PostgreSQL, Foreign Keys are defined by constraints. These constraints indicate the match of
values from a column in the child table to a column in the parent table.
So, in summary, the purpose of a foreign key is to maintain referential integrity between parent
and child tables.
Column Constraints:
Column constraints, set the rules for the column.
- Not Null - The value of the column cannot be NULL.
- The data that gets inserted into the column cannot be NULL (blank)
Unique - the values inserted into the column must be unique across the whole table.
There can be an exception to this if NULL values are inserted into this table, because
PostgreSQL considers each NULL value as unique - Keep in mind that is standard isn’t
shared across every SQL engine.
PRIMARY KEY - this constraint is the combination of NOT NULL and UNIQUE constraints.
When defining one column as the PRIMARY KEY, you would use a column level
constraint.
However, when the primary key contains multiple columns, you’ll need to use the table
level constraint.
CHECK - this enables to check a condition when you insert of update data.
REFERENCES - constrains the value of the column that exists in column in another table
Table Constraints.
So, in PostgreSQL as well as other SQL engines, we have table constraints that allow us to
control how data is managed by more than one column. A list of columns, if you will.
UNIQUE(column_list) - To force the value stored in the columns listed inside the parentheses
to be unique.
PRIMARY KEY(column_list) - To define the primary key that consists of multiple columns.
SQL Queries 26
CHECK(condition) - To check a condition when inserting or updating data.
REFERENCES - To constrain the value stored in the column that must exist in a column in
another table.
CREATE TABLE
Let’s make sure we can create a DATABASE using the psql shell.
So, first, open up a terminal and fire up a psql shell.
psql
Hackerman=#
\l;
You should get some like of read out like this print in your console:
-----------------------+-----------+----------+-------------+-----------
--+----- Hackerman | Hackerman | UTF8 | en_US.UTF-8 | en
_US.UTF-8 | booklist | postgres | UTF8 | en_US.UTF-8 |
en_US.UTF-8 | dvdrental | postgres | UTF8 | en_US.UTF-8
| en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF
-8 | en_US.UTF-8 |(4 rows)
Now that you’ve created a database and you can list it to see what it looks like in the shell, let’s
connect to it with \c followed by the name of your database:
\c DBNAME;
Awesome! We’re going to create some tables and then some rows inside of those tables.
Now you can create a table simply with the following syntax:
SQL Queries 27
CREATE TABLE table_name -- First your specify the table name after CREAT
E TABLE(column_name TYPE column_contraint, -- column names separated by
commatable_constraint)
NOTE You can add the following syntax if you’re unsure if you’ve created a table or not:
This will ensure your PostgreSQL engine skips over creating the table if it already exists.
Insert Data
First let’s look at an example:
Then we use the keyword VALUES , and we add the values in the same order
as we specified the columns in the
INSERT INTO statement.
We could also insert multiple rows into a table at the same time using the same syntax
like so:
You can also easily insert a copy of a row from one table into another table like this:
SQL Queries 28
What if we needed to learn how to UPDATE ?
Let’s learn how to update the values of the rows in the columns.
UPDATE table_name
SET column_1=value1,
column_2, value2,
WHERE condition;
First, we use the UPDATE syntax to tell our SQL engine we’ll be updating.
Then we use the SET statement in conjunction with the column name setting it to the new
value.
Now, if we don’t add a WHERE statement to specify with row we want to update, this query will
update all the rows in the columns.
We can also see the all the data we updated get returned back to us by using a RETURNING * .
UPDATE table_name
SET column_1=value1,
column_2=value2
WHERE condition RETURNING *;
You can also just request certain columns back instead of returning everything:
UPDATE table_name
SET column=valueWHERE condition RETURNING column, column_2, column_3;
DELETE
Okay, so we’ve covered quite a bit, what if we needed to DELETE rows in a table?
Just like the other methods we learned, the syntax for this operation is pretty straight forward.
NOTE if you omit the WHERE clause, all of the rows in the table will be deleted.
The DELETE statement will return the number of rows deleted, but if no rows were deleted, the
statement will return 0.
SQL Queries 29
ALTER TABLE
What if we needed to alter a table such as change the name of the table, or add / remove an
entire column?
PostgreSQL gives us a few very powerful queries we can do to accomplish this very task.
SQL Queries 30