SQL Expression Cheat Sheet in Common Concepts

 Expressions in Querying

We can write expressions in various SQL clauses. Expressions can comprise of various data types like integers, floats, strings, datetime, etc.

Let's learn more about expressions using the following database.

Database

The IMDb stores various movies, actors and cast information.

Using Expressions in SELECT Clause

  1. Get profits of all movies.

Note: Consider profit as the difference between collection and budget.

SELECT
id, name, (collection_in_cr-budget_in_cr) as profit
FROM
movie;
SQL
Output
idnameprofit
1The matrix40.31
2Inception67.68
3The Dark Knight82.5
.........
Note
We use "||" operator to concatenate strings in sqlite3
  1. Get the movie
    name
    and
    genre
    in the following format:
    movie_name - genre
    .
SELECT
name || " - " || genre AS movie_genre
FROM
movie;
SQL
Output
movie_genre
The Matrix - Sci-fi
Inception - Action
The Dark Knight - Drama
Toy Story 3 - Animation
...

Using Expressions in WHERE Clause

  1. Get all the movies with a profit of at least 50 crores.
SELECT
*
FROM
movie
WHERE
(collection_in_cr - budget_in_cr) >= 50;
SQL
Output
idnamegenrebudget_in_crcollection_in_crratingrelease_date
2InceptionAction16.083.688.82010-07-14
3The Dark KnightAction18.0100.59.02008-07-16
4Toy Story 3Animation20.0106.78.52010-06-25
.....................

Using Expressions in UPDATE Clause

  1. Scale down ratings from 10 to 5 in movie table.
UPDATE movie
SET rating = rating/2;
SQL

You can check the updation of movie ratings by retriving the data from the table.

Expressions in HAVING Clause

Get all the genres that have average profit greater than 100 crores.

SELECT
genre
FROM
movie
GROUP BY
genre
HAVING
AVG(collection_in_cr - budget_in_cr) >= 100;
SQL
Output
genre
Action
Animation
Mystery
...

Try it Yourself!

Question 1 Get the profit of every movie with

rating
greater than 8.0

Expected Output
idnamegenrebudget_in_crcollection_in_crratingrelease_dateprofit
1The MatrixSci-fi6.346.438.71999-04-3140.13
2InceptionAction1683.688.82010-07-1667.68
3The Dark KnightDrama18100.592008-07-1882.5
........................

Question 2 Get all the movies having a

profit
of at least 30 crores, and belong to "Action", "Animation" or "Drama" genres.

Expected Result
idnamegenrebudget_in_crcollection_in_crratingrelease_date
2InceptionAction1683.688.82010-07-16
3The Dark KnightDrama18100.592008-07-18
4Toy Story 3Animation20106.78.52010-06-25
.....................

Question 3 Scale up the ratings from 5 to 100 in the movie table.

MOVIE

idnamegenrebudget_in_crcollection_in_crratingrelease_date
1The MatrixSci-fi6.346.438.71999-04-31
2InceptionAction1683.688.82010-07-16
3The Dark KnightDrama18100.592008-07-18
4Toy Story 3Animation20106.78.52010-06-25
5Shutter IslandMystery100722.57.52003-05-27
6Sherlock HolmesMystery5226682010-11-25
7Iron ManAction756009.22008-06-14
8DeadpoolAction1006009.12016-05-17
9Deadpool 2Action1202008.32018-11-20
10Iron Man 2Action2008008.52010-05-24
11ThorAction1502507.82010-03-24
12Avengers: Age of UltronAction9.5106.52015-06-17
13Spider-Man: HomecomingAction10122.52010-06-04
14Black PantherAction25505.62016-05-19
15Avengers: EndgameAction1256008.82018-04-15
16Avengers: Infinity WarAction2053008.62017-03-06
17How to Train Your DragonAnimation1203259.52010-06-13
18The God FatherDrama25627.11972-06-24
19The God Father 2Drama30457.51974-11-23
20InterstellarSci-fi3006008.82010-03-18
ACTOR
actor_idnameage
1Keanu Reeves46
2Carrie-Anne Moss53
3Christopher Nolan60
4Leonardo DiCaprio50
5Lee Unkrich60
6Robert Downey Jr55
7Ryan Reynolds45
8Mark Ruffalo35
9Tom Holland30
10Smith54
11Marion Cotillard45
12Tom Hardy Eames54
13Joaquin Phoenix60
14Maggie Gyllenhaal55
15Michelle Williams42
16Mark Strong34
17Rachel McAdams40
18Morena Baccarin36
19Josh Brolin35
20James Caan30
21Jude Law30
22Chris Evans34
CAST
actor_idmovie_idrole
11Lead Actor
22Lead Actress
42Lead Actor
33Lead Actor
45Lead Actor
66Lead Actor
78Lead Actor
79Lead Actor
710Lead Actor
913Lead Actor
610Lead Actor
615Lead Actor
815Lead Actor
101Supporting Actor
112Lead Actress
122Supporting Actor
133Supporting Actor
143Lead Actress
166Supporting Actor
155Lead Actress
189Lead Actress
199Supporting Actor
2018Supporting Actor
216Lead Actor
2215Lead Actor

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form