SQL Functions Cheat Sheet in Common Concepts

 SQL Functions

SQL provides many built-in functions to perform various operations over data that is stored in tables. Let's look at a few most commonly used functions in the industry using the following database.

Database The IMDb dataset stores the information of movies, actors and cast.

Schema

Date Functions

strftime()

strftime() function is used to extract month, year, etc. from a date/datetime field based on a specified format.

Syntax

strftime(format, field_name)
SQL

Example

Get the movie title and release year for every movie in the database

SELECT name, strftime('%Y', release_date)
FROM
movie;
SQL

In the above query, we extract year from

release_date
by writing
strftime('%Y', release_date)
in
SELECT
clause.

Let's understand various formats in date functions with an example. Consider the datetime

2021-02-28 08:30:05

formatdescriptionoutput formatExample
%dDay of the month01 - 3128
%HHour00 - 2408
%mMonth01 - 1202
%jDay of the year001 - 36559
%MMinute00-5930
............

Example

Get the number of movies released in each month of the year 2010

SELECT
strftime('%m', release_date) as month,
COUNT(*) as total_movies
FROM
movie
WHERE
strftime('%Y', release_date) = '2010'
GROUP BY
strftime('%m', release_date);
SQL
Output
monthtotal_movies
032
051
063
....

As the above example, using strftime(), we can perform weekly, monthly or annual analysis deriving finer insights from the data.

Try it Yourself!

Question 1 Get the number of "Action" movies released in the year 2010.

Expected Output
total_movies
4

Question 2 Get all the movies that are released in summer, i.e., between April and June.

Expected Output
name
The Matrix
Toy Story 3
Shutter Island
...

Question 3 Get the month in which the highest number of movies are released.

Expected Output
monthtotal_movies
066

CAST Function

CAST function is used to typecast a value to a desired data type.

Syntax

CAST(value AS data_type);
SQL

Example

Get the number of movies released in each month of the year 2010

SELECT strftime('%m', release_date) as month,
COUNT(*) as total_movies
FROM
movie
WHERE
CAST(strftime('%Y', release_date) AS INTEGER) = 2010
GROUP BY
strftime('%m', release_date);
SQL
Output
monthtotal_movies
032
051
063
....

Here,

CAST(strftime('%Y', release_date) AS INTEGER)
converts the year in string format to integer format.

Try it Yourself!

Question 1 Get all the leap years in the database. An year can be marked as a leap year if

1 .It is divisible by 4 and not divisible by 100 2. Or if it is divisible by 400

Expected Output
year
1972
2008
2016

Other Common Functions

Arithmetic Functions

SQL FunctionBehavior
FLOORRounds a number to the nearest integer below its current value
CEILRounds a number to the nearest integer above its current value
ROUNDRounds a number to a specified number of decimal places

You can refer the following table to further understand how floor, ceil and round work in general.

2.33.94.05.5
FLOOR2345
CEIL3446
ROUND2446

Let's understand how these functions can be used.

Examples

  1. Fetch the ceil, floor and round (to 1 decimal) values of the collections of all movies.
SELECT
name,
ROUND(collection_in_cr, 1) AS RoundedValue,
CEIL(collection_in_cr) AS CeilValue,
FLOOR(collection_in_cr) AS FloorValue
FROM
movie;
SQL
Output
nameRoundedValueCeilValueFloorValue
The Matrix46.44746
Inception83.78483
The Dark Knight100.5101100
............

String Functions

SQL FunctionBehavior
UPPERConverts a string to upper case
LOWERConverts a string to lower case

When you are not sure about the case (upper/lower) of the movie name, you can write a query as below to search for all the avengers movies irrespective of the case.

SELECT
name
FROM
movie
WHERE UPPER(name) LIKE UPPER("%avengers%");
SQL
Output
name
Avengers: Age of Ultron
Avengers: Endgame
Avengers: Infinity War
Note
Usually, UPPER() AND LOWER() functions can help you to perform case-insensitive searches.

Try it Yourself!

Question 1 For each movie, get the ceil, floor and round(to 1 decimal) values of budget.

Expected Output
nameround_valueceil_valuefloor_value
The Matrix6.376
Inception161616
The Dark Knight181818
............

Question 2 Get all the movie names that are released in 2010 and belong to "Action" genre.

Note:

Try using the sql functions learnt so far

Expected Output
movie_name
Inception
Iron Man 2
Thor
Spider-Man: Homecoming
...

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form