You are on page 1of 33

SESSION 4

BASIC ARITHMETIC
In addition to using aggregate functions, you can perform basic arithmetic with symbols like +, -, *, and /.
SELECT (4 * 3);
12
SELECT (4 / 3);
1
If you divide an integer by an integer, you want to get an integer back. 
For more precision when dividing, you can add decimal places to your numbers. 

SELECT (4.0 / 3.0);

1.333
AS SIMPLE AS ALIASING
SELECT MAX(budget) FROM films;

SELECT MAX(budget), MAX (duration) FROM films;

O/P will be two column names with the word max

SQL allows you to do something called aliasing. Aliasing simply means you assign a temporary name
to something. To alias, you use the AS keyword. Aliases are helpful for making results more readable!
id title release_year country duratio language certification gross budget
n
1 Throughout the Ages 1916 USA 123 null Not Rated null 385907
2 Over the Hill to the Poorhouse 1920 USA 110 null null 3000000 100000
3 The Big Parade 1925 USA 151 null Not Rated null 245000
4 Metropolis 1927 Germany 145 German Not Rated 26435 6000000

5 Pandora's Box 1929 Germany 110 German Not Rated 9950 null

6 The Broadway Melody 1929 USA 100 English Passed 2808000 379000

7 Hell's Angels 1930 USA 96 English Passed null 3950000

8 The Farewell to Palms 1932 USA 79 English Unrated null 800000

1. Get the title and net profit (the amount a film grossed, minus its budget) for all films. Alias the net profit
as net_profit.
2. Get the title and duration in hours for all films. The duration is in minutes, so you'll need to divide by 60.0 to get the
duration in hours. Alias the duration in hours as duration_hours
3. Get the average duration in hours for all films, aliased as avg_duration_hours.
id title release_year country duratio language certification gross budget
n
1 Throughout the Ages 1916 USA 123 null Not Rated null 385907
2 Over the Hill to the Poorhouse 1920 USA 110 null null 3000000 100000
3 The Big Parade 1925 USA 151 null Not Rated null 245000
4 Metropolis 1927 Germany 145 German Not Rated 26435 6000000

5 Pandora's Box 1929 Germany 110 German Not Rated 9950 null

6 The Broadway Melody 1929 USA 100 English Passed 2808000 379000

7 Hell's Angels 1930 USA 96 English Passed null 3950000

8 The Farewell to Palms 1932 USA 79 English Unrated null 800000

1. Get the title and net profit (the amount a film grossed, minus its budget) for all films. Alias the net profit as net_profit.
select title, (gross-budget) as net_profit from films;

2. Get the title and duration in hours for all films. The duration is in minutes, so you'll need to divide by 60.0 to get the duration in hours. Alias the duration in
hours as duration_hours
select title, (duration/60.0) as duration_hours from films;
3. Get the average duration in hours for all films, aliased as avg_duration_hours.
select avg(duration)/60.0 as avg_duration_hours from films;
id title release_year country duratio language certification gross budget
n
1 Throughout the Ages 1916 USA 123 null Not Rated null 385907
2 Over the Hill to the Poorhouse 1920 USA 110 null null 3000000 100000
3 The Big Parade 1925 USA 151 null Not Rated null 245000
4 Metropolis 1927 Germany 145 German Not Rated 26435 6000000

5 Pandora's Box 1929 Germany 110 German Not Rated 9950 null

6 The Broadway Melody 1929 USA 100 English Passed 2808000 379000

7 Hell's Angels 1930 USA 96 English Passed null 3950000

8 The Farewell to Palms 1932 USA 79 English Unrated null 800000

1. Get the number of years between the newest film and oldest film. Alias the result as difference

2. Get the number of decades the films table covers. Alias the result as number_of_decades.
id title release_year country duratio language certification gross budget
n
1 Throughout the Ages 1916 USA 123 null Not Rated null 385907
2 Over the Hill to the Poorhouse 1920 USA 110 null null 3000000 100000
3 The Big Parade 1925 USA 151 null Not Rated null 245000
4 Metropolis 1927 Germany 145 German Not Rated 26435 6000000

5 Pandora's Box 1929 Germany 110 German Not Rated 9950 null

6 The Broadway Melody 1929 USA 100 English Passed 2808000 379000

7 Hell's Angels 1930 USA 96 English Passed null 3950000

8 The Farewell to Palms 1932 USA 79 English Unrated null 800000

1. Get the number of years between the newest film and oldest film. Alias the result as difference
select Max (release_year)-min(release_year) as difference from films;
2. Get the number of decades the films table covers. Alias the result as number_of_decades.
select (Max(release_year)-Min(release_year))/ 10.0 as number_of_decades from films;
IN CLASS EXERCISE
1. Get the title from films where the release year is 2000 or 2012. Order the results
by the release year.

2. Get all details for all films except those released in 2015 and order them by
duration.

3. Get the title and gross earnings for movies which begin with the letter 'M' and
order the results alphabetically.
IN CLASS EXERCISE :
SOLUTION
1. Get the title from films where the release year is 2000 or 2012. Order the results by the
release year.
select title from films where release_year in (2000,2012) order by release_year;

2. Get all details for all films except those released in 2015 and order them by duration.
select * from films where NOT release_year=2015 order by duration;

3. Get the title and gross earnings for movies which begin with the letter 'M' and order the
results alphabetically.
select title, gross from films where title like 'M%' order by title;
PRACTICE EXERCISE
Table : IMDB
id film_id num_user num_critic imdb_score num_votes facebook_likes
1 3934 588 432 7.1 203461 46000
2 3405 285 267 6.4 149998 0
3 478 65 29 3.2 8465 491

1. Get the IMDB score and film ID for every film from the reviews table, sorted from highest to
lowest score.
2. Get the title for every film, in reverse order. (Refer Films Table)
3. Get the title and duration for every film, in order of longest duration to shortest. (Refer Films Table)
PRACTICE EXERCISE -
SOLUTIONS
1. Get the IMDB score and film ID for every film from the reviews table, sorted from
highest to lowest score.
SELECT imdb_score, film_id from reviews order by imdb_score desc;

2. Get the title for every film, in reverse order.


SELECT title from films order by title desc;

3. Get the title and duration for every film, in order of longest duration to shortest.
SELECT title, duration from films order by duration desc;
Table : People

id name birthdate deathdate

PRACTICE EXERCISE 1
2
50 Cent
Bravo James
1975-07-06
1963-04-04
null
null
3 Arvind Naik null null
4 Angelo Desa 1978-02-09 null
1. Get the birth date and name of people in the people table, in 5 A.J. DeLucia null null
order of when they were born and alphabetically by name.
6 A.J. Langer 1974-05-22 null

select birthdate, name from people order by birthdate, name; 7 Aaliyah 1979-01-16 2001-08-25
8 Vinay Chauhan 1979-10-07 null
2. Get the release year, duration, and title of films ordered by
9 Beena Anjaria null null
their release year and duration. (Refer to films table);
10 Shaurya Malkan 2013-10-23 null

select release_year, duration, title from films order by 11 Haren Harids null null
release_year,duration; 12 Brinda Sampat 1985-08-11 null
13 Babloo Gill null null
3. Get certifications, release years, and titles of films ordered
14 Nirav Shah 1974-01-12 null
by certification (alphabetically) and release year.
15 Aaron Stanford 1976-12-27 null
select certification, release_year, title from films order by
certification, release_year;
Table : People

PRACTICE EXERCISE 1
id name
50 Cent
birthdate
1975-07-06
deathdate
null

SOLUTION 2
3
Bravo James
Arvind Naik
1963-04-04
null
null
null
4 Angelo Desa 1978-02-09 null
1. Get the birth date and name of people in the people table, in 5 A.J. DeLucia null null
order of when they were born and alphabetically by name.
6 A.J. Langer 1974-05-22 null

select birthdate, name from people order by birthdate, name; 7 Aaliyah 1979-01-16 2001-08-25
8 Vinay Chauhan 1979-10-07 null
2. Get the release year, duration, and title of films ordered by
9 Beena Anjaria null null
their release year and duration. (Refer to films table);
10 Shaurya Malkan 2013-10-23 null

select release_year, duration, title from films order by 11 Haren Harids null null
release_year,duration; 12 Brinda Sampat 1985-08-11 null
13 Babloo Gill null null
3. Get certifications, release years, and titles of films ordered
14 Nirav Shah 1974-01-12 null
by certification (alphabetically) and release year.
15 Aaron Stanford 1976-12-27 null
select certification, release_year, title from films order by
certification, release_year;
GROUP BY
You'll need to aggregate results. Example, you might
want to count the number of male and female
employees in your company. Here, what you want is to
group all the males together and count them, and group
all the females together and count them. In
SQL, GROUP BY allows you to group a result by one
or more columns

SELECT sex, count(*) FROM employees GROUP BY


sex;
Commonly, GROUP BY is used with aggregate
functions like COUNT() or MAX(). Note that GROUP
BY always goes after the FROM clause!
Combining aggregate functions with GROUP BY can yield some powerful results
Note that you can combine GROUP BY with ORDER BY to group your results,
calculate something about them, and then order your results. For example,

SELECT sex, count(*) FROM employees GROUP BY sex ORDER BY count


DESC;
Group BY helps to summarize query results at sub-total level
e.g. "find the number of customers in each country".
EXAMPLES
What is the average order What is the average order size for each
size? (refer to order table) salesperson? (refer to order table)
SELECT AVG(AMOUNT) SELECT REP, AVG(AMOUNT)
FROM ORDERS
FROM ORDERS
GROUP BY REP

AVG(AMOUNT) The
REP AVG(AMOUNT) second query produces several
------------ ---- ------------
summary rows—one row for
each group, summarizing the
$8,256.37 101 $8,876.00 orders taken by a single
salesperson.
102 $5,694.00
A query that includes the GROUP BY clause is
called a grouped query because it groups the
data from its source tables and produces a
HOW DOES THIS WORK ?
single summary row for each row group.
The columns named in the GROUP BY clause
are called the grouping columns of the query,
because they determine how the rows are
divided into groups.
IN CLASS EXERCISE !
1. Get the release year and count of films released in each year.

2. Get the release year and average duration of all films, grouped by release year.

3. Get the release year and largest budget for all films, grouped by release year.
IN CLASS EXERCISE
--SOLUTION
1. Get the release year and count of films released in each year.
select release_year, count(title) from films group by release_year;

2. Get the release year and average duration of all films, grouped by release year.
select release_year, avg (duration) from films group by release_year;

3. Get the release year and largest budget for all films, grouped by release year.
select release_year, max(budget) from films group by release_year;
PRACTICE EXERCISE – REFER
TO FILMS TABLE
1. Get the release year and lowest gross earnings per release year.

2. Get the language and total gross amount films in each language made.

3. Get the country and total budget spent making movies in each country.

4. Get the release year, country, and highest budget spent making a film for each
year, for each country. Sort your results by release year and country.

5. Get the country, release year, and lowest amount grossed per release year per
country. Order your results by country and release year.
PRACTICE EXERCISE – REFER
TO FILMS TABLE
1. Get the release year and lowest gross earnings per release year.
select release_year, min (gross) from films group by release_year;
2. Get the language and total gross amount films in each language made.
select language, sum (gross) from films group by language;
3. Get the country and total budget spent making movies in each country.
select country, sum (budget) from films group by country;
4. Get the release year, country, and highest budget spent making a film for each year, for each
country. Sort your results by release year and country.
select release_year, country, max(budget) from films group by release_year, country order by
release_year, country;
5. Get the country, release year, and lowest amount grossed per release year per country. Order
your results by country and release year.
select country, release_year, min(gross) from films group by release_year, country order by
country, release_year;
EXAMPLE 1
1. What is the range of assigned quotas in each office?
SELECT REP_OFFICE, MIN(QUOTA), MAX(QUOTA)
FROM SALESREPS
GROUP BY REP_OFFICE
EXAMPLE 2
2. How many salespeople are assigned to each office?

SELECT REP_OFFICE, COUNT(*)


FROM SALESREPS
GROUP BY REP_OFFICE
3. How many different customers are
served by each salesperson?
select count (DISTINCT cust_num), cust_rep from
customers group by cust_rep;
4. Calculate the total orders for each
customer of each salesperson.
Also this means Group the orders by
salesperson and by customer
SELECT REP, CUST,
SUM(AMOUNT)
FROM ORDERS
GROUP BY REP,
CUST
HAVING
To filter based on the result of an aggregate function, you need another way! That's
where the HAVING clause comes in. For example,
The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
SELECT column_name(s) In how many different years
FROM table_name were more than 100 movies
WHERE condition released?
GROUP BY column_name(s) SELECT release_year
HAVING condition FROM films
ORDER BY column_name(s); GROUP BY release_year
HAVING COUNT(title) > 100;
EXAMPLE 1
1. What is the average order size for
each salesperson whose orders total
more than $30,000?
Select rep, avg (amount)
from orders
Group by rep
Having sum(amount) > 30000;
Custo Customer Contact Address City PostalCode Country
merI Name Name
D

EXAMPLE 2 1 Alfreds
Futterkiste
Maria
Anders
Obere Str.
57
Berlin 12209 Germany

2 Prerna P Shah Andheri Mumbai 400056 India


3 Amit Amit D Mataderos Pune 05023 India
1. Get the customers in each Deshpande
country. Only include
4 Around the Thomas 120 London WA1 1DP UK
countries with more than 2 Horn Hardy Hanover
customers.
5 Berglunds Christin Berguvsvä Luleå S-958 22 Germany
Select count(customerID), snabbköp a gen 8
country Berglun
from customers d
group by country
Having count(customer_id)>2;
EXAMPLE 3
Write a query that returns the average budget and average gross earnings for films in each year
after 1990, if the average budget is greater than $60 million.
1. Get the release year, budget and gross earnings for each film in the films table.
2. Modify your query so that only records with a release_year after 1990 are included.
3. Remove the budget and gross columns, and group your results by release year.
4. Modify your query to include the average budget and average gross earnings for the results
you have so far. Alias the average budget as avg_budget; alias the average gross earnings
as avg_gross.
5. Modify your query so that only years with an average budget of greater than $60 million are
included.
6. Finally, modify your query to order the results from highest average gross earnings to lowest.
1. Get the release year, budget and gross earnings for each film in the films table.
select release_year, budget, gross from films;
2. Modify your query so that only records with a release_year after 1990 are included.
select release_year,budget, gross from films where release_year>1990;
3. Remove the budget and gross columns, and group your results by release year.
select release_year from films where release_year>1990 group by release_year;
4. Modify your query to include the average budget and average gross earnings for the results you have so far.
Alias the average budget as avg_budget; alias the average gross earnings as avg_gross.
select release_year, avg(budget) as avg_budget, avg (gross) as avg_gross from films where release_year>1990
group by release_year;
5. Modify your query so that only years with an average budget of greater than $60 million are included.
select release_year, avg(budget) as avg_budget, avg (gross) as avg_gross from films where release_year>1990
group by release_year having avg(budget) > 60000000;
6. Finally, modify your query to order the results from highest average gross earnings to lowest.
select release_year, avg(budget) as avg_budget, avg (gross) as avg_gross from films where release_year>1990
group by release_year having avg(budget) > 60000000 order by avg (gross) desc;
TEST YOURSELF
Get the country, average budget, and average gross take of countries that have made more
than 10 films. Order the result by country name, and limit the number of results displayed to
5. You should alias the averages as avg_budget and avg_gross respectively.

select country, avg (budget) as avg_budget, avg (gross) as avg_gross from films
group by country
having count(title)>10
order by country
limit 5;
EXAMPLE
2. For each office with two or more
people, compute the total quota and
total sales for all salespeople who work
in the office.

SELECT CITY, SUM(QUOTA),

SUM(SALESREPS.SALES)

FROM OFFICES, SALESREPS

WHERE OFFICE = REP_OFFICE

GROUP BY CITY

HAVING COUNT(*) >= 2;

You might also like