Professional Documents
Culture Documents
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.
1.333
AS SIMPLE AS ALIASING
SELECT MAX(budget) FROM films;
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
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
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
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
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;
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
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
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?
EXAMPLE 2 1 Alfreds
Futterkiste
Maria
Anders
Obere Str.
57
Berlin 12209 Germany
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.
SUM(SALESREPS.SALES)
GROUP BY CITY