You are on page 1of 7

ASSIGMENT -4

In Operator
The IN operator in SQL filters the result set based on a list of discrete values. The IN operator is always used with
the WHERE clause.
Syntax
Below is the syntax for the IN operator when the possible values are listed out directly.
SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...);
The number of values in the parenthesis can be one or more, with each values separated by comma. Values can be
numerical or string characters. If there is only one value inside the parenthesis, this commend is equivalent to,
WHERE "column_name" = 'value1'

Please note that the IN operator cannot be used if the filtering criteria is a continuous range. For example, if we
are looking for any value that is between 0 and 1, we cannot use the IN operator because it is not possible to list
every possible value between 0 and 1.

BETWEEN operator
The BETWEEN operator is used when the filtering criteria is a continuous range with a maximum value and a minimum
value. It is always used in the WHERE clause.
Syntax
The syntax for the BETWEEN operator is as follows:
SELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2';
This will select all rows whose column has a value between 'value1' and 'value2.'

BETWEEN is an inclusive operator, meaning that 'value1' and 'value2' are included in the result. If we wish to exclude
'value1' and 'value2' but include everything in between, we need to change the query to the following:
SELECT "column_name"
FROM "table_name"
WHERE ("column_name" > 'value1')
AND ("column_name" < 'value2');

GROUP BY
The GROUP BY clause is used to tell SQL what level of granularity the aggregate function should be calculated in. The
level of granularity is represented by the columns in the SELECT statement that are not aggregate functions.
Syntax
The syntax for GROUP BY is,
SELECT "column_name1", "function type" ("column_name2")
FROM "table_name"
GROUP BY "column_name1";
More than one column can be specified in the GROUP BY clause, and more than one function can be included.
GROUP BY is the command that can trip up many beginners, as it is often possible to have a SQL statement with the
correct GROUP BY syntax, yet get the wrong results. A good rule of thumb when using GROUP BY is to include all
the non-aggregate function columns in the SELECT statement in the GROUP BY clause.
Examples
Table Store_Information
Store_Name Product_ID Sales Txn_Date
Los Angeles 1 1500 Jan-05-1999
Los Angeles 2 500 Jan-05-1999
San Diego 1 250 Jan-07-1999
Los Angeles 1 300 Jan-08-1999
Boston 1 700 Jan-08-1999

Example 1: GROUP BY a single column


We want to find total sales for each store. To do so, we would key in,
SELECT Store_Name, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name;
Result:
Store_Name SUM(Sales)
Los Angeles 2300
San Diego 250
Boston 700
Example 2: GROUP BY multiple columns
In Example 1, there is only one column associated with GROUP BY. It is possible to have two or
more columns associated with GROUP BY.
We want to find total sales for each product at each store. To do so, we would key in,
SELECT Store_Name, Product_ID, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name, Product_ID;
Result:
Store_Name Product_ID SUM(Sales)
Los Angeles 1 1800
Los Angeles 2 500
San Diego 1 250
Boston 1 700
Example 3: GROUP BY multiple columns and multiple functions
We want to find total sales and the average sales for each product at each store. To do so, we would
key in,
SELECT Store_Name, Product_ID, SUM(Sales), AVG(Sales)
FROM Store_Information
GROUP BY Store_Name, Product_ID;
Result:
Store_Name Product_ID SUM(Sales) AVG(Sales)
Los Angeles 1 1800 900
Los Angeles 2 500 500
San Diego 1 250 250
Boston 1 700 700
Example 4: Group by month / date / week
A common use of the GROUP BY function is on a time period, which can be month, week, day, or
even hour. This type of query is often combined with the ORDER BY keyword to provide a query
result that shows a time series.
For example, to find total daily sales from Store_Information, we use the following SQL:
SELECT Txn_Date, SUM(Sales)
FROM Store_Information
GROUP BY Txn_Date
ORDER BY Txn_Date;
Result:
Txn_Date SUM(Sales)
Jan-05-1999 2000
Jan-07-1999 250
Jan-08-1999 1000

HAVING clause
The HAVING clause is used to filter the result set based on the result of an aggregate function. It is typically located near
or at the end of the SQL statement.
HAVING is often coupled with the presence of the GROUP BY clause, although it is possible to have
a HAVING clause without the GROUP BY clause.
Syntax
The syntax for HAVING is,
SELECT ["column_name1"], "function type" ("column_name2")
FROM "table_name"
[GROUP BY "column_name1"]
HAVING (arithmetic function condition);
The brackets around "column_name1" and GROUP BY "column_name1" means that they are optional.
Note: We may select zero, one, or more columns in addition to the aggregate function. If we do select any column outside
of the aggregate function, there is no need for the GROUP BY clause.

Example
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
To see only the stores with sales over $1,500, we would type,
SELECT Store_Name, SUM(Sales)
FROM Store_Information
GROUP BY Store_Name
HAVING SUM(Sales) > 1500;
Result:
Store_Name SUM(Sales)
Los Angeles 1800

UNION query
The purpose of the SQL UNION query is to combine the results of two queries together while removing
duplicates. In other words, when using UNION, only unique values are returned (similar
to SELECT DISTINCT).
Syntax
The syntax of UNION in SQL is as follows:
[SQL Statement 1]
UNION
[SQL Statement 2];
The columns selected in [SQL Statement 1] and [SQL Statement 2] need to be of the same data type
for UNION to work.
Example
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
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 is a sales transaction, we use the following SQL statement:
SELECT Txn_Date FROM Store_Information
UNION
SELECT Txn_Date FROM Internet_Sales;
Result:
Txn_Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999

INTERSECT command
The INTERSECT command in SQL combines the results of two SQL statement and returns only data that are
present in both SQL statements.
INTERSECT can be thought of as an AND operator (value is selected only if it appears in both statements),
while UNION and UNION ALL can be thought of as an OR operator (value is selected if it appears in either
the first or the second statement).
Syntax
The syntax for INTERSECT is as follows:
[SQL Statement 1]
INTERSECT
[SQL Statement 2];
The columns selected in [SQL Statement 1] and [SQL Statement 2] need to be of the same data type
for INTERSECT to work.

Example
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
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 following 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.

MINUS command
The MINUS command operates on two SQL statements. It takes all the results from the 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 results not present in the first SQL statement, such results are ignored.
Syntax
The syntax for MINUS is as follows:
[SQL Statement 1]
MINUS
[SQL Statement 2];

The columns selected in [SQL Statement 1] and [SQL Statement 2] need to be of the same data type for MINUS to work.
Example
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
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 following SQL statement:
SELECT Txn_Date FROM Store_Information
MINUS
SELECT Txn_Date FROM Internet_Sales;
Result:
Txn_Date
Jan-05-1999
Jan-08-1999
'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_Sales, 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 of MINUS.

TOP keyword
The TOP keyword restricts the number of results returned from a SQL statement in Microsoft SQL Server.
Syntax
The syntax for TOP is as follows:
SELECT TOP [TOP argument] "column_name"
FROM "table_name";
where [TOP argument] can be one of two possible types:
1. [N]: The first N records are returned.
2. [M] PERCENT: The number of records corresponding to M% of all qualifying records are returned.

Examples

Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-08-1999
Example 1: [TOP argument] is an integer
To show the two highest sales amounts in Table Store_Information, we key in,
SELECT TOP 2 Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
Result:
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
Boston 700 Jan-08-1999
Example 2: [TOP argument] is a percentage
To show the top 25% of sales amounts from Table Store_Information, we key in,
SELECT TOP 25 PERCENT Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
Result:
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
Exercises: Answer the following Queries:
For these exercises, assume we have a table called Users and User Sales with the following data:
1) Table Users
First_Name Last_Name Birth_Date Gender Join_Date
Sophie Lee Jan-05-1960 F Apr-05-2015
Richard Brown Jan-07-1975 M Apr-05-2015
Jamal Santo Oct-08-1983 M Apr-09-2015
Casey Healy Sep-20-1969 M Apr-09-2015
Jill Wilkes Nov-20-1979 F Apr-15-2015
User Sales
First_Name Last_Name Birth_Date Gender Joi n_Date Total_Sales
Sophie Lee Jan-05-1960 F Apr-05-2015 500
Richard Brown Jan-07-1975 M Apr-05-2015 200
Jamal Santo Oct-08-1983 M Apr-09-2015 350
Casey Healy Sep-20-1969 M Apr-09-2015 80
Jill Wilkes Nov-20-1979 F Apr-15-2015 210

2) Retrieve all the records from users table where gender in F or M.


3) Retrieve all the records from users table where Join_Date in Apr-05-2015, Apr-09-2015.
4) Retrieve all the records from users table where gender in M and Join_Date is Apr-05-2015.
5) Retrieve all the records from user_Sales table where between 200 and 300.

6) Retrieve all the records from user_Sales table where Join_Date between Apr-05-2015, Apr-09-2015.

7) Create a Table Region_Sales


Region Year Orders Total_Sales
West 2013 1560 325000
West 2014 1820 380000
North 2013 790 148000
North 2014 995 185000
East 2013 1760 375000
East 2014 2220 450000
South 2013 1790 388000
South 2014 1695 360000
8) Write a SQL statement that calculates the total sales amount for each region.
9) Write a SQL statement that calculates the average annual sales amount for just the East region
and the West region.
10) Retrieve Region, Sum of order from region_Sales group by region having sum of orders of
greater than 2500.
11) Retrieve Region, Sum of order from region_Sales where total sales are lesser than 385000
group by region having sum of orders of greater than 2500.

You might also like