1. The document discusses various SQL clauses - JOIN, ORDER BY, LIMIT - and provides examples of how to use them. JOIN combines records from multiple tables, including CROSS JOIN, INNER JOIN, and OUTER JOIN. ORDER BY sorts the results based on specified columns. LIMIT restricts the number of rows returned, optionally using OFFSET to skip rows.
2. Exercises are provided to practice these clauses by querying sample database tables, such as returning the Cartesian product using CROSS JOIN, sorting results, and limiting rows returned.
Packaging the Monolith - PHP Tek 2024 (Breaking it down one bite at a time)
Assignment 4
1. Assignment 4
As discussed in the previous assignment, the SELECT statement was dissected, but not all
functionalities were tackled. Today we will analyze the ORDER BY, LIMIT and JOIN keywords
how they influence the base query and what data manipulation possibilities they entail.
In the current assignment only, the marked commands will be used.
1. SELECT
Please refer to Assignment 3 for a brief recap of the SELECT statement. This the core select
clause.
2. 1. JOIN clause
SQLite Joins clause is used to combine records from two or more tables in a database. A JOIN
is a means for combining fields from two tables by using values common to each.
SQL defines three major types of joins −
• The CROSS JOIN
• The INNER JOIN
• The OUTER JOIN
Before we proceed, let's consider two tables COMPANY and DEPARTMENT. We already have
seen INSERT statements to populate COMPANY table. So just let's assume the list of records
available in COMPANY table.
3. We also need the DEPARTMENT table.
v The CROSS JOIN
CROSS JOIN matches every row of the first table with every row of the second table. If the
input tables have x and y row, respectively, the resulting table will have x*y row. Because
CROSS JOINs have the potential to generate extremely large tables, care must be taken to
only use them when appropriate.
Using the syntax for CROSS JOIN
SELECT ... FROM table1 CROSS JOIN table2 ...
We will obtain for the DEPARTMENT.EMP_ID, zCOMPANY.NAME, DEPARTMENT.DEPT columns
the following results.
EMP_ID NAME DEPT
---------- ---------- ----------
1 Paul IT Billing
2 Paul Engineering
7 Paul Finance
1 Allen IT Billing
2 Allen Engineering
7 Allen Finance
1 Teddy IT Billing
2 Teddy Engineering
7 Teddy Finance
1 Mark IT Billing
2 Mark Engineering
7 Mark Finance
1 David IT Billing
2 David Engineering
7 David Finance
1 Kim IT Billing
2 Kim Engineering
7 Kim Finance
1 James IT Billing
2 James Engineering
7 James Finance
4. v The INNER JOIN
INNER JOIN creates a new result table by combining column values of two tables (table1 and
table2) based upon the join-predicate. The query compares each row of table1 with each row
of table2 to find all pairs of rows which satisfy the join-predicate. When the join-predicate is
satisfied, the column values for each matched pair of rows of A and B are combined into a
result row.
An INNER JOIN is the most common and default type of join. You can use INNER keyword
optionally.
Following is the syntax of INNER JOIN –
SELECT ... FROM table1 [INNER] JOIN table2 ON
conditional_expression ...
To avoid redundancy and keep the phrasing shorter, INNER JOIN conditions can be declared
with a USING expression. This expression specifies a list of one or more columns.
SELECT ... FROM table1 JOIN table2 USING (column1, ...) ...
A NATURAL JOIN is similar to a JOIN...USING, only it automatically tests for equality
between the values of every column that exists in both tables −
SELECT ... FROM table1 NATURAL JOIN table2...
Based on the above tables, you can write an INNER JOIN as follows –
SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
The above query will produce the following result −
EMP_ID NAME DEPT
---------- ---------- ----------
1 Paul IT Billing
2 Allen Engineering
7 James Finance
v The OUTER JOIN
OUTER JOIN is an extension of INNER JOIN. Though SQL standard defines three types of
OUTER JOINs: LEFT, RIGHT, and FULL, SQLite only supports the LEFT OUTER JOIN.
OUTER JOINs have a condition that is identical to INNER JOINs, expressed using an ON,
USING, or NATURAL keyword. The initial results table is calculated the same way. Once the
primary JOIN is calculated, an OUTER JOIN will take any un-joined rows from one or both
tables, pad them out with NULLs, and append them to the resulting table.
Following is the syntax of LEFT OUTER JOIN –
SELECT ... FROM table1 LEFT OUTER JOIN table2 ON
conditional_expression ...
5. Based on the above tables, you can write an outer join as follows −
sqlite> SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN
DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
The above query will produce the following result −
EMP_ID NAME DEPT
---------- ---------- ----------
1 Paul IT Billing
2 Allen Engineering
Teddy
Mark
David
Kim
7 James Finance
2. ORDER BY clause
If a SELECT statement that returns more than one row does not have an ORDER BY clause,
the order in which the rows are returned is undefined. Or, if a SELECT statement does have
an ORDER BY clause, then the list of expressions attached to the ORDER BY determine the
order in which rows are returned to the user.
Rows are first sorted based on the results of evaluating the left-most expression in the ORDER
BY list, then ties are broken by evaluating the second left-most expression and so on. The
order in which two rows for which all ORDER BY expressions evaluate to equal values are
returned is undefined. Each ORDER BY expression may be optionally followed by one of the
keywords ASC (smaller values are returned first) or DESC (larger values are returned first).
If neither ASC or DESC are specified, rows are sorted in ascending (smaller values first) order
by default.
SQLite considers NULL values to be smaller than any other values for sorting purposes. Hence,
NULLs naturally appear at the beginning of an ASC order-by and at the end of a DESC order-
by. This can be changed using the "ASC NULLS LAST" or "DESC NULLS FIRST" syntax.
Each ORDER BY expression is processed as follows:
6. Ø If the ORDER BY expression is a constant integer K then the expression is
considered an alias for the K-th column of the result set (columns are numbered
from left to right starting with 1).
Ø If the ORDER BY expression is an identifier that corresponds to the alias of one of
the output columns, then the expression is considered an alias for that column.
Ø Otherwise, if the ORDER BY expression is any other expression, it is evaluated and
the returned value used to order the output rows. If the SELECT statement is a
simple SELECT, then an ORDER BY may contain any arbitrary expressions.
However, if the SELECT is a compound SELECT, then ORDER BY expressions that
are not aliases to output columns must be exactly the same as an expression used
as an output column.
3. LIMIT clause
The LIMIT clause is used to place an upper bound on the number of rows returned by the
entire SELECT statement.
In a compound SELECT, only the last or right-most simple SELECT may contain a LIMIT clause.
In a compound SELECT, the LIMIT clause applies to the entire compound, not just the final
SELECT.
Any scalar expression may be used in the LIMIT clause, so long as it evaluates to an integer
or a value that can be losslessly converted to an integer. If the expression evaluates to a
NULL value or any other value that cannot be losslessly converted to an integer, an error is
returned. If the LIMIT expression evaluates to a negative value, then there is no upper bound
on the number of rows returned. Otherwise, the SELECT returns the first N rows of its result
set only, where N is the value that the LIMIT expression evaluates to. Or, if the SELECT
statement would return less than N rows without a LIMIT clause, then the entire result set is
returned.
The expression attached to the optional OFFSET clause that may follow a LIMIT clause must
also evaluate to an integer, or a value that can be losslessly converted to an integer. If an
expression has an OFFSET clause, then the first M rows are omitted from the result set
returned by the SELECT statement and the next N rows are returned, where M and N are the
values that the OFFSET and LIMIT clauses evaluate to, respectively. Or, if the SELECT would
return less than M+N rows if it did not have a LIMIT clause, then the first M rows are skipped
and the remaining rows (if any) are returned. If the OFFSET clause evaluates to a negative
value, the results are the same as if it had evaluated to zero.
Instead of a separate OFFSET clause, the LIMIT clause may specify two scalar expressions
separated by a comma. In this case, the first expression is used as the OFFSET expression
7. and the second as the LIMIT expression. This is counter-intuitive, as when using the OFFSET
clause, the second of the two expressions is the OFFSET and the first the LIMIT. This reversal
of the offset and limit is intentional - it maximizes compatibility with other SQL database
systems. However, to avoid confusion, programmers are strongly encouraged to use the form
of the LIMIT clause that uses the "OFFSET" keyword and avoid using a LIMIT clause with a
comma-separated offset.
Excercises:
Use the file provided in the assignment SQL_SAMPLE_DATABASE.txt to create and populate
the tables needed for the exercises, it will be used for the next exercises.
1. Write an SQL statement that returns the Cartesian product of rows from the tables in
the join. Observe the results and specify what happened.
(Hint CROSS JOIN)
2. Write an SQL joins two tables by their common column names. Observe the results
and specify what happened.
(Hint NATURAL JOIN)
3. Write an SQL clause that combines columns from correlated tables that show the name
of the employee and the job title. Observe the results and specify what happened.
(Hint INNER JOIN on JOB_ID)
4. Write an SQL query that fetches all the rows from the specified fields of the left-hand
table. Observe the results and specify what happened.
(Hint LEFT OUTER JOIN employee -> jobs)
5. Same as above but make sure that all entries retrieved from the query do not have
null inside jobs.JOB_TITLE. Is the result similar to a query already called, if so, why?
6. Write an SQL query to retrieve all entries that have salary over 5000 and sort them
descending
(Hint ORDER BY)
7. Write an SQL query to return all entries from employee table that have ‘JOB_ID’ equal
to SH_CLERK but limit to only 10 of them
(Hint LIMIT)
8. Write an SQL query to return all entries from employee table that have ‘JOB_ID’ equal
to SH_CLERK but limit to only the last 10
(HINT OFFSET)