CS232L-Lab#02
CS232L-Lab#02
Objective
The objective of this session is to
Instructions
Open the handout/lab manual in front of a computer in the lab during the session.
Practice each new command by completing the examples and exercise.
Turn-in the answers for all the exercise problems as your lab report.
When answering problems, indicate the commands you entered and the output displayed.
1
CS232-L – LAB 2
The WHERE clause appears right after the FROM clause of the SELECT statement.
The WHERE clause uses the condition to filter the rows returned from the SELECT clause.
The condition must evaluate to true, false, or unknown. It can be a boolean expression or a
combination of boolean expressions using the AND and OR operators.
The query returns only rows that satisfy the condition in the WHERE clause. In other words,
only rows that cause the condition evaluates to true will be included in the result set.
PostgreSQL evaluates the WHERE clause after the FROM clause and before the SELECT:
If you use column aliases in the SELECT clause, you cannot use them in the WHERE clause.
Besides the SELECT statement, you can use the WHERE clause in
the UPDATE and DELETE statement to specify rows to be updated or deleted.
2
CS232-L – LAB 2
3
CS232-L – LAB 2
The following example finds customers whose first name and last name are Jamie and rice by
using the AND logical operator to combine two Boolean expressions:
Output:
Output:
You can use several conditions in one WHERE clause using the AND and OR operators.
4
CS232-L – LAB 2
Let’s suppose we have an Employee Table then the query given above will return the
employee number , employee name , job and salalry form employee table where the salary of
the employee is greater than and equal to 1100 and job title is ’CLERK’.
Output:
5
CS232-L – LAB 2
Using the WHERE clause with the not equal operator (<>) example
This example finds customers whose first names are Brand or Brandy and last names are
not Motley:
Output:
If you want to match a string with any string in a list, you can use the IN operator.
For example, the following statement returns customers whose first name is Ann, or Anne,
or Annie:
Output:
6
CS232-L – LAB 2
Output:
The % is called a wildcard that matches any string. The 'Ann%' pattern matches any string
that starts with 'Ann'.
Using the WHERE clause with the BETWEEN operator example
The following example finds customers whose first names start with the letter A and contains
3 to 5 characters by using the BETWEEN operator.
7
CS232-L – LAB 2
In this example, we used the LENGTH() function gets the number of characters of an input
string.
Null value
If a row lacks the data value for a particular column, that value is said to be null, or to contain
null. A null value is a value that is unavailable, unassigned, unknown, or inapplicable. A null
value is not the same as zero or a space. Zero is a number and space is a character. If any
column value in an arithmetic expression is null, the result is null. For example if you attempt
to perform division with zero, you get an error. However if you divide a number by null, the
result is a null or unknown.
A column alias allows you to assign a column or an expression in the select list of
a SELECT statement a temporary name. The column alias exists temporarily during the
execution of the query.
8
CS232-L – LAB 2
In this syntax, the column_name is assigned an alias alias_name. The AS keyword is optional
so you can omit it like this:
The following syntax illustrates how to set an alias for an expression in the SELECT clause:
The main purpose of column aliases is to make the headings of the output of a query more
meaningful.
This query assigned the surname as the alias of the last_name column:
9
CS232-L – LAB 2
The DISTINCT clause is used in the SELECT statement to remove duplicate rows from a
result set. The DISTINCT clause keeps one row for each group of duplicates.
The DISTINCTclause can be applied to one or more columns in the select list of
the SELECT statement.
In this statement, the values in the column1 column are used to evaluate the duplicate.
If you specify multiple columns, the DISTINCT clause will evaluate the duplicate based on
the combination of values of these columns.
10
CS232-L – LAB 2
Let’s create a new table called distinct_demo and insert data into it for practicing
the DISTINCT clause.
First, use the following CREATE TABLE statement to create the distinct_demo table that
consists of three columns: id, bcolor and fcolor.
Second, insert some rows into the distinct_demo table using the following INSERT statement:
Third, query the data from the distinct_demo table using the SELECT statement:
Output:
11
CS232-L – LAB 2
In the database world, NULL means missing information or not applicable. NULL is not a
value, therefore, you cannot compare it with any other values like numbers or strings. The
comparison of NULL with a value will always result in NULL, which means an unknown
result.
In addition, NULL is not equal to NULL so the following expression returns NULL:
NULL = NULL
Assuming that you have a contacts table that stores the first name, last name, email, and
phone number of contacts. At the time of recording the contact, you may not know the
contact’s phone number.
To deal with this, you define the phone column as a nullable column and insert NULL into
the phone column when you save the contact information.
The following statement inserts two contacts, one has a phone number and the other does not:
12
CS232-L – LAB 2
To find the contact who does not have a phone number you may come up with the following
statement:
The statement returns no row. This is because the expression phone = NULL in
the WHERE clause always returns false.
Even though there is a NULL in the phone column, the expression NULL = NULL returns
false. This is because NULL is not equal to any value even itself.
To check whether a value is NULL or not, you use the IS NULL operator instead:
value IS NULL
So to get the contact who does not have any phone number stored in the phone column, you
use the following statement instead:
13
CS232-L – LAB 2
PostgreSQL UPDATE
The PostgreSQL UPDATE statement allows you to modify data in a table. The following
illustrates the syntax of the UPDATE statement:
UPDATE table_name
SET column1 = value1,
column2 = value2,
...
WHERE condition;
In this syntax:
First, specify the name of the table that you want to update data after
the UPDATE keyword.
Second, specify columns and their new values after SET keyword. The columns that
do not appear in the SET clause retain their original values.
Third, determine which rows to update in the condition of the WHERE clause.
The WHERE clause is optional. If you omit the WHERE clause, the UPDATE statement will
update all rows in the table.
When the UPDATE statement is executed successfully, it returns the following command tag:
UPDATE count
The count is the number of rows updated including rows whose values did not change.
PostgreSQL UPDATE examples
Let’s take some examples of using the PostgreSQL UPDATE statement.
The following statements create a table called courses and insert some data into it:
The following statement returns the data from the courses table:
SELECT * FROM courses;
14
CS232-L – LAB 2
The following statement uses the UPDATE statement to update the course with id 3. It
changes the published_date from NULL to '2020-08-01'.
To drop a table from the database, you use the DROP TABLE statement as follows:
In this syntax:
First, specify the name of the table that you want to drop after the DROP
TABLE keywords.
Second, use the IF EXISTS option to remove the table only if it exists.
If you remove a table that does not exist, PostgreSQL issues an error. To avoid this situation,
you can use the IF EXISTS option.
In case the table that you want to remove is used in other objects such as views, triggers,
functions, and stored procedures, the DROP TABLE cannot remove the table. In this case,
you have two options:
The CASCADE option allows you to remove the table and its dependent objects.
The RESTRICT option rejects the removal if there is any object depends on the table.
The RESTRICT option is the default if you don’t explicitly specify it in the DROP
TABLE statement.
To remove multiple tables at once, you can place a comma-separated list of tables after
the DROP TABLE keywords:
Let’s take some examples of using the PostgreSQL DROP TABLE statement
16
CS232-L – LAB 2
17
CS232-L – LAB 2
To remove all data from a table, you use the DELETE statement. However, when you use
the DELETE statement to delete all data from a table that has a lot of data, it is not efficient.
In this case, you need to use the TRUNCATE TABLE statement:
The TRUNCATE TABLE statement deletes all data from a table without scanning it. This is
the reason why it is faster than the DELETE statement.
18
CS232-L – LAB 2
The following example uses the TRUNCATE TABLE statement to delete all data from
the invoices table:
To remove all data from multiple tables at once, you separate each table by a comma (,) as
follows:
TRUNCATE TABLE
table_name1,
table_name2,
...;
Code language: SQL (Structured Query Language) (sql)
For example, the following statement removes all data from invoices and customers tables:
In practice, the table you want to truncate often has the foreign key references from other
tables that are not listed in the TRUNCATE TABLE statement.
By default, the TRUNCATE TABLE statement does not remove any data from the table that
has foreign key references.
To remove data from a table and other tables that have foreign key reference the table, you
use CASCADE option in the TRUNCATE TABLE statement as follows :
The following example deletes data from the invoices table and other tables that reference
the invoices table via foreign key constraints:
The CASCADE option should be used with further consideration or you may potentially
delete data from tables that you did not want.
19
CS232-L – LAB 2
By default, the TRUNCATE TABLE statement uses the RESTRICT option which prevents
you from truncating the table that has foreign key constraint references.
20