SQL Part 2
SQL Part 2
The MINUS command operates on two SQL statements. It takes all the results from t
he first SQL statement, and then subtract out the ones that are present in the second
SQL statement to get the final result set. If the second SQL statement includes result
s not present in the first SQL statement, such results are ignored.
Syntax
[SQL Statement 1]
MINUS
[SQL Statement 2];
The columns selected in [SQL Statement 1] and [SQL Statement 2] need to be of the s
ame data type for MINUS to work.
Example
Table Internet_Sales
Txn_Date Sales
Jan-07-1999 250
Jan-10-1999 535
Jan-11-1999 320
Jan-12-1999 750
To find all the dates where there are store sales but no internet sales, we use the foll
owing SQL statement:
SELECT Txn_Date FROM Store_Information
MINUS
SELECT Txn_Date FROM Internet_Sales;
Result:
Txn_Date
Jan-05-199
9
Jan-08-199
9
'Jan-05-1999', 'Jan-07-1999';, and 'Jan-08-1999' are the distinct values returned from
SELECT Txn_Date FROM Store_Information. Out of the three dates, 'Jan-07-1999' is
also returned from the second SQL statement,SELECT Txn_Date FROM Internet_Sale
s, so it is excluded from the final result set.
Please note that the MINUS command will only return distinct values.
Some databases may use EXCEPT instead ofMINUS. Please check the documentation
for your specific database for the correct usage.
To retrieve the two highest sales amounts in Table Store_Information, we key in,
Table Internet_Sales
Txn_Date Sales
Jan-07-1999 250
Jan-10-1999 535
Jan-11-1999 320
Jan-12-1999 750
To find out all the dates where there are both store sales and internet sales, we use the followi
ng SQL statement:
SELECT Txn_Date FROM Store_Information
INTERSECT
SELECT Txn_Date FROM Internet_Sales;
Result:
Txn_Date
Jan-07-1999
Please note that the INTERSECT command will only return distinct values.
Assume we have two tables: The first table isUser_Address, which maps each user to a ZIP code
; the second table is User_Score, which records all the scores of each user. The question is, how
to write a SQL query to find the number of users who scored higher than 200 for each ZIP code?
Without using an inline view, we can accomplish this in two steps:
Query 1
CREATE TABLE User_Higher_Than_200
SELECT User_ID, SUM(Score) FROM User_Score
GROUP BY User_ID
HAVING SUM(Score) > 200;
Query 2
SELECT a2.ZIP_CODE, COUNT(a1.User_ID)
FROM User_Higher_Than_200 a1, User_Address a2
WHERE a1.User_ID = a2.User_ID
GROUP BY a2.ZIP_CODE;
In the above code, we introduced a temporary table, User_Higher_Than_200, to store the list o
f users who scored higher than 200.User_Higher_Than_200 is then used to join to theUser_Add
ress table to get the final result.
We can simplify the above SQL using the inline view construct as follows:
Query 3
SELECT a2.ZIP_CODE, COUNT(a1.User_ID)
FROM
(SELECT User_ID, SUM(Score) FROM User_Score GROUP BY User_ID HAVING S
UM(Score) > 200) a1,
User_Address a2
WHERE a1.User_ID = a2.User_ID
GROUP BY a2.ZIP_CODE;
The code that is in red represents an inline view. There are two advantages on using inline view
here:
1. We do not need to create the temporary table. This prevents the database from having too
many objects, which is a good thing as each additional object in the database costs resources to
manage.
2. We can use a single SQL query to accomplish what we want.
Notice that we treat the inline view exactly the same as we treat a table. Comparing Query 2 an
d Query 3, we see that the only difference is that we replace the temporary table name in Quer
y 2 with the inline view statement in Query 3. Everything else stays the same.
A subquery is a SQL statement that has another SQL query embedded in the WHER
E or theHAVING clause.
Syntax
The syntax for a subquery when the embedded SQL statement is part of the WHER
E condition is as follows:
SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE "condition");
[Comparison Operator] could be equality operators such as =, >, <, >=, <=. It can also
be a text operator such as "LIKE". The portion in red is considered as the "inner query
," while the portion in green is considered as the "outer query."
Examples
Table Store_Information
Table Geography
To use a subquery to find the sales of all stores in the West region, we use the followi
ng SQL statement:
Result:
SUM (Sales)2050
In this example, instead of joining the two tables directly and then adding up only th
e sales amount for stores in the West region, we first use the subquery to find out w
hich stores are in the West region, and then we sum up the sales amount for these st
ores.
Notice that in this example, the inner query and the outer query are independent of
each other. This type of subquery is called a simple subquery.
If the inner query is dependent on the outer query, we will have a correlated subque
ry. An example of a correlated subquery is shown below:
Result:
SUM (Sales)2750
Here, the inner query is used to make sure that SQL only sums up sales amount from
stores that appear in both the Store_Information and theGeography tables.
Notice the WHERE clause in the inner query, where the condition involves a table fro
m the outer query.
@@@@@@@@@
SQL > Advanced SQL > Intersect
The INTERSECT command in SQL combines the results of two SQL statement and retu
rns only data that are present in both SQL statements.
Syntax
[SQL Statement 1]
INTERSECT
[SQL Statement 2];
The columns selected in [SQL Statement 1] and [SQL Statement 2] need to be of the s
ame data type for INTERSECT to work.
Example
Table Store_Information
Store_NameSalesTxn_DateLos Angeles1500Jan-05-1999San Diego250Jan-07-1999Los
Angeles300Jan-08-1999Boston700Jan-08-1999
Table Internet_Sales
Txn_DateSalesJan-07-1999250Jan-10-1999535Jan-11-1999320Jan-12-1999750
To find out all the dates where there are both store sales and internet sales, we use t
he following SQL statement:
Result:
Txn_DateJan-07-1999
Please note that the INTERSECT command will only return distinct values.
@@@@@@@
SQL > Advanced SQL > Top
The TOP keyword restricts the number of results returned from a SQL statement in
Microsoft SQL Server.
Syntax
Examples
Table Store_Information
Result:
Store_NameSalesTxn_DateLos Angeles1500Jan-05-1999Boston700Jan-08-1999
To show the top 25% of sales amounts from Table Store_Information, we key in,
Result:
Store_NameSalesTxn_DateLos Angeles1500Jan-05-1999
CASE is used to provide if-then-else type of logic to SQL. There are two formats: The f
irst is a Simple CASE expression, where we compare an expression to static values. Th
e second is a Searched CASEexpression, where we compare an expression to one or
more logical conditions.
To multiply the sales amount from 'Los Angeles' by 2 and the sales amount from 'San
Diego' by 1.5 while keeping the sales amount for other stores the same, we would us
e the following SQL statement using CASE:
"New Sales" is the name given to the column with the CASE statement. This is an exa
mple of a simpleCASE expression, because the conditions listed, 'Los SQL > Advanced
SQL > Exists
EXISTS is a Boolean operator used in a subqueryto test whether the inner query retur
ns any row. If it does, then the outer query proceeds. If not, the outer query does not
execute, and the entire SQL statement returns nothing.
Syntax
SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS
(SELECT *
FROM "table_name2"
WHERE "condition");
Please note that instead of *, you can select one or more columns in the inner query.
The effect will be identical.
Example
Table Store_Information
SUM(Sales)2750
At first, this may appear confusing, because the subquery includes the [region_name
= 'West'] condition, yet the query summed up sales for stores in all regions. Upon clo
ser inspection, we find that since the subquery returns more than zero row, the EXIS
TS condition is true, and the rows returned from the query "SELECT SUM(Sales) FRO
M Store_Information" become the final result.
Syntax
where [Initial_Value] is the first value of the primary key, and [Interval] is the interval
between two consecutive identity values. If no [Initial_Value] or [Interval] is specifie
d, the default for both is 1. In other words, the primary key for the first row would be
1, and subsequent rows would get a primary key value that is 1 larger than the previ
ous row.
Example
Assume we want to create a table that consists of a primary key, last name, and first
name. We use the following SQL:
CREATE TABLE USER_TABLE
(Userid int PRIMARY KEY IDENTITY(2,1),
Last_Name nvarchar(50),
First_Name nvarchar(50));
Table USER_TABLE
UseridLast_NameFirst_Name2WashingtonGeorge
Table USER_TABLE
UseridLast_NameFirst_Name2WashingtonGeorge3JeffersonThomas
userid for the second row is 3 because it is 1 larger than the previous Userid, which is
2.
DECODE is a function in Oracle and is used to provide if-then-else type of logic to SQL
. It is not available in MySQL or SQL Server.
Syntax
"search_value" is the value to search for, and "result" is the value that is displayed.
[ ] means that the "search_value_npair can occur zero, one, or more times.
Example
Table Store_Information
To display 'LA' for 'Los Angeles', 'SF' for 'San Francisco', 'SD' for 'San Diego', and 'Othe
rs' for all other cities, we use the following SQL,
"Area" is the name given to the column that theDECODE function operates on.
Result:
AreaSalesTxn_DateLA1500Jan-05-1999SD250Jan-07-1999SF300Jan-08-1999Others70
0Jan-08-1999
To achieve what DECODE does in MySQL and SQL Server, we would use the CASE fun
ction.
The CAST function in SQL converts data from one data type to another. For example,
we can use the CAST function to convert numeric data into character string data.
Syntax
where [data type] is a valid data type in the RDBMS you are working with.
Examples
Table Student_Score
Column NameData TypeStudentIDintegerFirst_Namechar(20)Scorefloat
Table Student_Score
StudentIDFirst_NameScore1Jenny85.22Bob92.53Alice904James120.1Example 1
Result:
First_NameInt_ScoreJenny85Bob92Alice90James120
In Example 1, we use the CAST function to convert the Score column from type FLOA
T to INTEGER. When we do this, different RDMBS have different rules on how to han
dle the numbers after the decimal point. In the above example, the numbers after th
e decimal point are always truncated.
Example 2
Result:
First_NameChar_ScoreJenny85.Bob92.Alice90 James120
In Example 2, we use the CAST function to convert the SCORE column from type FLO
AT to CHAR(3). When we do this, we only take the first three characters. So, if there
are more than three characters, everything after the first three characters is discarde
d.
Oracle uses the concept of SEQUENCE to create numerical primary key values as we
add rows of data into a table. Whereas numerical primary key population for MySQL
and SQL Server is tied to individual tables, in Oracle the SEQUENCEconstruct is create
d separately and is not tied to an individual table.
{Initial_Value} is the starting value of the sequence, and {interval} is the interval betw
een consecutive sequence numbers. Both [START WITH] and [INCREMENT BY] are op
tional fields. If they are not specified, the default {Initial_Value} and {interval} are bot
h 1.
Example
Table USER_TABLE
UseridintegerLast_Namevarchar(50)First_Namevarchar(50)
We specify that we want to use the sequence and the NEXTVAL function in the INSER
T INTOstatements in the following order:
Table USER_TABLE
UseridLast_NameFirst_Name5WashingtonGeorge10JeffersonThomas
Table NEW_USER
UseridLast_NameFirst_Name15AdamsJohn
Result:
Store_NameNew SalesTxn_DateLos Angeles3000Jan-05-1999San Diego375Jan-07-19
99San Francisco300Jan-08-1999Boston700Jan-08-1999
SELECT CASE
WHEN "condition1" THEN "result1"
WHEN "condition2" THEN "result2"
...
[ELSE "resultN"]
END
FROM "table_name";
The ELSE clause is optional. "Condition" can consist of one or more logical statement
s.
We use the same Store_Information above. If we want to define the status of a store
's sale based on the following rules:
If Sales >= 1,000, it's a "Good Day"If Sales >= 500 and < 1,000, it's an "OK Day"If Sales
< 500, it's a "Bad Day"
Result:
Note that a simple CASE expression is a special case of a searched CASE expression. A
s an example, the following two CASE expressions are identical: