My Sqleeeeeeeeeeee
My Sqleeeeeeeeeeee
Introduction to Databases
Concepts in Focus
Data
Database
Database Management System(DBMS)
Advantages
Types of Databases
Relational Database
Non-Relational Database
Data
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=6b5cde2d-9f90-48c6-b262-2555c5d1… 1/4
23/09/2021, 17:52 Revolutionizing the Job Market | NxtWave
Examples:
Database
A software that is used to easily store and access data from the database in a secure way.
Advantages
Durability and Availability: Durable and provides access to all the clients at any point in time.
Performance: Quickly accessible to all the clients(applications and stakeholders).
Types of Databases
There are different types of databases based on how we organize the data.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=6b5cde2d-9f90-48c6-b262-2555c5d1… 2/4
23/09/2021, 17:52 Revolutionizing the Job Market | NxtWave
Relational Database
Non-Relational Database
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=6b5cde2d-9f90-48c6-b262-2555c5d1… 3/4
23/09/2021, 17:52 Revolutionizing the Job Market | NxtWave
Note
Relational DBMS
Examples: Oracle, PostgreSQL, MySQL, SQLite, SQL Server, IBM DB2, etc.
Non-Relational DBMS
MARKED AS COMPLETE
Submit Feedback
Notes
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=6b5cde2d-9f90-48c6-b262-2555c5d1… 4/4
23/09/2021, 17:55 Revolutionizing the Job Market | NxtWave
Introduction to SQL
We have already learnt that databases and DBMS are key to organising and analysing data for business uses.
From here on, let’s get busy working around with databases using SQL!
SQL provides multiple clauses (commands) to perform various operations like create, retrieve, update and delete the data.
The first step towards working with the database would be creating a table.
Create Table
Syntax
SQL
1 CREATE TABLE table_name (
2 column1 type1,
3 column2 type2,
4 ...
5 );
Here,
type1 and type2 in the syntax are the datatypes of column1 and column2 respectively. Datatypes that are supported in SQL
are mentioned below.
Example
Create a
column_name data_type
name VARCHAR(200)
age INT/INTEGER
score INT/INTEGER
SQL
1 CREATE TABLE player (
2 name VARCHAR(200),
3 age INTEGER,
4 score INTEGER
5 );
We can check the details of the created table at any point in time using the
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 1/3
23/09/2021, 17:55 Revolutionizing the Job Market | NxtWave
Assume that we have to build a database that stores all the information about the students in a school, subjects, exam schedules, etc. Lets build a
few tables to store the data!
details data_type
name VARCHAR(200)
date_of_birth DATE
address TEXT
details data_type
name VARCHAR(200)
course VARCHAR(200)
exam_date_time DATETIME
duration_in_sec INT
pass_percentage FLOAT
Data Types
Float FLOAT
String VARCHAR
Text TEXT
Date DATE
Time TIME
Datetime DATETIME
Boolean BOOLEAN
Note
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 2/3
23/09/2021, 17:55 Revolutionizing the Job Market | NxtWave
PRAGMA
PRAGMA TABLE_INFO command returns the information about a specific table in a database.
Syntax
SQL
1 PRAGMA TABLE_INFO(table_name);
Example
Output
0 employee_id INTEGER 0 0
1 name VARCHAR(200) 0 0
2 salary INTEGER 0 0
Note
If the given table name does not exist, PRAGMA TABLE_INFO doesn’t give any result.
Try it Yourself!
Try checking out the information of the tables that you have created above.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 3/3
23/09/2021, 17:56 Revolutionizing the Job Market | NxtWave
Inserting Rows
Syntax
SQL
1 INSERT INTO
2 table_name (column1, column2,..., columnN)
3 VALUES
4 (value11, value12,..., value1N),
5 (value21, value22,..., value2N),
6 ...;
Any number of rows from 1 to n can be inserted into a specified table using the above syntax.
Database
Let's learn more about the INSERT clause by going hands-on on the
player and match_details tables that store the details of players and matches in a tournament respectively.
player table stores the name, age and score of players.
match_details table stores name of team, opponent team name, place, date and match result
Examples
Upon executing the above code, both the entries would be added to the
player table.
Note
1. Boolean values can be either given as (TRUE or FALSE) or (1 or 0). But in the database, the values are stored
as 1 or 0.
2. Date object is represented as: ‘YYYY-MM-DD’
3. Datetime object is represented as: ‘YYYY-MM-DD HH:MM:SS’
Possible Mistakes
Mistake 1
The number of values that we're inserting must match with the number of column names that are specified in the query.
SQL
1 INSERT INTO
2 player(name, age, score)
3 VALUES
4 ("Virat", 31);
SQL
1 Error: 2 values for 3 columns
Mistake 2
SQL
1 Error: no such table: players_information
Mistake 3
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 2/3
23/09/2021, 17:56 Revolutionizing the Job Market | NxtWave
SQL
1 Error: 2 values for 3 columns
Mistake 4
While inserting data, be careful with the datatypes of the input values. Input value datatype should be same as the
column datatype.
SQL
1 INSERT INTO
2 player(name, age, score)
3 VALUES
4 ("Virat", 31, "Hundred");
Warning
If the datatype of the input value doesn't match with the datatype of column, SQLite doesn't raise an error.
Try it Yourself!
Three new players have joined the tournament. Try inserting the players' data in the player table.
Ram 28 70
Sita 25 30
Ravi 30 53
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 3/3
23/09/2021, 17:56 Revolutionizing the Job Market | NxtWave
Retrieving Data
Database
The database consists of a
table that stores the details of players who are a part of a tournament.
player player table stores the
name, age and score of players.
Let's explore more about the SELECT clause using the database!
To retrieve the data of only specific columns from a table, add the respective column names in the SELECT clause.
Syntax
SQL
1 SELECT
2 column1,
3 column2,
4 ...,
5 columnN
6 FROM
7 table_name;
Example
Output
name age
Virat 32
Rakesh 39
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 1/3
23/09/2021, 17:56 Revolutionizing the Job Market | NxtWave
name age
Sai 47
--- ---
Sometimes, we may want to select all the columns from a table. Typing out every column name, for every time we have
to retrive the data, would be a pain.
We have a shortcut for this!
Syntax
SQL
1 SELECT *
2 FROM table_name;
Example
player table.
SQL
1 SELECT *
2 FROM player;
Output
Virat 32 50
Rakesh 39 35
Sai 47 30
Syntax
SQL
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 2/3
23/09/2021, 17:56 Revolutionizing the Job Market | NxtWave
SQL
1 SELECT *
2 FROM table_name
3 WHERE condition;
WHERE clause specifies a condition that has to be satisfied for retrieving the data from a database.
Example
Get
name and age of the player whose name is "Ram" from the player table.
SQL
1 SELECT *
2 FROM player
3 WHERE name="Sai";
Output
Sai 47 30
Try it Yourself!
employee table that stores the employee_id , name and salary of employees. Let's fetch data for the
following queries.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 3/3
23/09/2021, 17:57 Revolutionizing the Job Market | NxtWave
Concepts in Focus
Update Rows
SQL – Case Insensitive
Database
The database consists of a
player table that stores the details of players who are a part of a tournament. player table stores the
name, age and score of players.
Update Rows
UPDATE clause is used to update the data of an existing table in database. We can update all the rows
or only specific rows as per the requirement.
Update All Rows
Syntax
SQL
1 UPDATE
2 table_name
3 SET
4 column1 = value1;
Example:
Update the
Syntax
SQL
1 UPDATE
2 table_name
3 SET
4 column1 = value1
5 WHERE
6 column2 = value2;
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 1/2
23/09/2021, 17:57 Revolutionizing the Job Market | NxtWave
Example
Update the
Try it Yourself!
student table that stores the information of name, percentage and scholarship amount of students.
Query 1
SQL
1 SELECT
2 *
3 FROM
4 player;
Query 2
SQL
1 select
2 *
3 from
4 player;
Note
Best Practice: Both Query 1 and Query 2 gives the same output. But, it is recommended to write keywords in upper case to
make the query more readable. Prefer Query 1 format over Query 2.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 2/2
23/09/2021, 17:57 Revolutionizing the Job Market | NxtWave
Concepts in Focus
Delete Rows
Drop Table
Database
The database consists of a
player table that stores the details of players who are a part of a tournament. player table stores the name,
age and score of players.
Delete Rows
Syntax
SQL
1 DELETE FROM
2 table_name;
Example
player table.
SQL
1 DELETE FROM
2 player;
Syntax
SQL
1 DELETE FROM
2 table_name
3 WHERE
4 column1 = value1;
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 1/2
23/09/2021, 17:57 Revolutionizing the Job Market | NxtWave
2 player
3 WHERE
4 name = "Shyam";
Warning
We can not retrieve the data once we delete the data from the table.
Drop Table
Syntax
SQL
1 DROP TABLE table_name;
Example
Delete
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 2/2
23/09/2021, 17:58 Revolutionizing the Job Market | NxtWave
Alter Table
ALTER clause is used to add, delete, or modify columns in an existing table. Let's learn more about ALTER
clause using the following database.
Database
The database consists of a
playertable that stores the details of players who are a part of a tournament. player table stores the name,
age and score of players.
Add Column
Syntax
SQL
1 ALTER TABLE
2 table_name
3 ADD
4 column_name datatype;
You can use PRAGMA TABLE_INFO(table_name) command to check the updated schema of the table.
Example
Note
Default values for newly added columns in the existing rows will be NULL.
Rename Column
Syntax
SQL
1 ALTER TABLE
bl
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 1/2
23/09/2021, 17:58 Revolutionizing the Job Market | NxtWave
2 table_name RENAME COLUMN c1 TO c2;
Example
Drop Column
Syntax
SQL
1 ALTER TABLE
2 table_name DROP COLUMN column_name;
Example
Note
Try it Yourself!
MARKED AS COMPLET
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=5b2c32ee-9e6e-49ba-bcda-339076b9… 2/2
23/09/2021, 17:58 Revolutionizing the Job Market | NxtWave
Comparison Operators
In a typical e-commerce scenario, users would generally filter the products with good ratings, or want to purchase the
products of a certain brand or of a certain price.
Let's see how comparison operators are used to filter such kind of data using the following database.
Database
The database contains a
table that stores the data of products like name, category, price, brand and rating. You can check the
product
schema and data of product table in the code playground.
Comparison Operators
Operator Description
= Equal to
Examples
1. Get all the details of the products whose category is "Food" from the product table.
SQL
1 SELECT
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 1/3
23/09/2021, 17:58 Revolutionizing the Job Market | NxtWave
2 *
3 FROM
4 product
5 WHERE
6 category = "Food";
Output
2. Get all the details of the products that does not belong to Food category from the product table.
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 category <> "Food";
Output
Similarly, we can use other comparison operators like greater than (>), greater than or equal to (>=), less than (<), less
than or equal to (<=) to filter the data as per the requirement.
Try it Yourself!
Put your learning into practice and try fetching the products based on the conditions mentioned.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 2/3
23/09/2021, 17:58 Revolutionizing the Job Market | NxtWave
3. brand is "Puma"
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 3/3
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
String Operations
Consider the case of e-commerce platforms. We generally search for the products on the basis of product name. But while
searching, we need not enter the full name. For example, typing “mobiles” in a search bar will fetch thousands of results. How
to get the data on the basis of only a part of the string? Let’s learn about it!
Database
The database contains a
table that stores the data of products like name, category, price, brand and rating. You can check the
product
schema and data of product table in the code playground.
LIKE Operator
LIKE operator is used to perform queries on strings. This operator is especially used in WHERE clause to
retrieve all the rows that match the given pattern.
We write
Percent sign ( % ) Represents zero or more characters ch% finds ch, chips, chocolate..
Underscore ( _ ) Represents a single character _at finds mat, hat and bat
Common Patterns
Syntax
SQL
1 SELECT
2 *
3 FROM
4 table name
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 1/4
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
4 table_name
5 WHERE
6 c1 LIKE matching_pattern;
Examples
1. Get all the products in the "Gadgets" category from the product table.
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 category LIKE "Gadgets";
Output
2. Get all the products whose name starts with "Bourbon" from the product table.
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 name LIKE "Bourbon%";
Here
% represents that, following the string "Bourbon", there can be 0 or more characters.
Output
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 2/4
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
3. Get all smart electronic products i.e., name contains "Smart" from the product table.
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 name LIKE "%Smart%";
Here,
% before and after the string "Smart" represents that there can be 0 or more characters succeeding or
preceding the string.
Output
4. Get all the products which have exactly 5 characters in brand from the product table.
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 brand LIKE "_____";
Output
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 3/4
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
Note
The percent sign(%) is used when we are not sure of the number of characters present in the string.
If we know the exact length of the string, then the wildcard character underscore(_) comes in handy.
Try it Yourself!
Put your learning into practice and try fetching the products based on the different patterns:
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 4/4
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
Logical Operators
So far, we've used comparison operators to filter the data. But in real-world scenarios, we often have to retrieve the data
using several conditions at once. For example, in the case of e-commerce platforms, users often search for something like:
Get shoes from the Puma brand, which have ratings greater than 4.0 and price less than 5000.
With logical operators, we can perform queries based on multiple conditions. Let's learn how with the following database.
Database
The database contains a
producttable that stores the data of products like name, category, price, brand and rating. You can check
the schema and data of product table in the code playground.
Operator Description
OR Used to fetch rows that satisfy at least one of the given conditions.
Syntax
SQL
1 SELECT
2 *
3 FROM
4 table_name
5 WHERE
6 condition1
7 operator condition2
8 operator condition3
9 ...;
Examples
Output
2. Ignore all the products with name containing "Cake" from the list of products.
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 NOT name LIKE "%Cake%";
Output
Try it Yourself!
Fetch all the products with price less than 20000 and brand is "Apple".
Fetch all the products with rating greater than 4.0 or brand is "Britannia".
Ignore all the products with category containing "Food" in product table.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 2/4
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
We can also use the combinations of logical operators to combine two or more conditions. These compound conditions
enable us to fine-tune the data retrieval requirements.
Precedence
When a query has multiple operators, operator precedence determines the sequence of operations.
Order of precedence:
NOT
AND
OR
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 3/4
23/09/2021, 17:59 Revolutionizing the Job Market | NxtWave
brand
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 brand = "Redmi"
7 AND rating > 4
8 OR brand = "OnePlus";
AND has the precedence over OR . So, the above query is equivalent to:
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 (brand = "Redmi"
7 AND rating > 4)
8 OR brand = "OnePlus";
Quick Tip
It is suggested to always use parenthesis to ensure correctness while grouping the conditions.
Try it Yourself!
Fetch all the products from "Clothing" category whose name does not contain "Jeans".
Fetch all the products from "Puma" and "Denim" brands excluding the products with name containing "Shirts".
Fetch all the products with price less than 100 or the products from "Food" category excluding the ones
with name containing "Chocolate".
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 4/4
23/09/2021, 18:00 Revolutionizing the Job Market | NxtWave
Consider the case of a typical e-commerce scenario. Users generally search for the products that belong to a list of brands, or
the products that lie within a particular price range.
In such scenarios, we use the IN operator to check if a value is present in the list of values. And, BETWEEN operator is used
to check if a particular value exists in the given range.
Let’s learn about these operators in detail using the following database.
Database
The database contains a
table that stores the data of products like name, category, price, brand and rating. You can check the
product
schema and data of product table in the code playground.
IN Operator
Retrieves the corresponding rows from the table if the value of column(c1) is present in the given values(v1,v2,..).
Syntax
SQL
1 SELECT
2 *
3 FROM
4 table_name
5 WHERE
6 c1 IN (v1, v2,..);
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 1/4
23/09/2021, 18:00 Revolutionizing the Job Market | NxtWave
product table, where the brand is either "Puma", "Mufti", "Levi's", "Lee" or "Denim".
SQL
1 SELECT
2 *
3 FROM
4 product
5 WHERE
6 brand IN ( "Puma", "Levi's", "Mufti", "Lee", "Denim");
Output
Try it Yourself!
Get all the products from product table, that belong to "Britannia", "Lay's", "Cadbury" brands from the "Food" category.
BETWEEN Operator
Retrieves all the rows from table that have cloumn(c1) value present between the given range(v1 and v2).
Syntax
SQL
1 SELECT
2 *
3 FROM
4 table_name
5 WHERE
6 c1 BETWEEN v1
7 AND v2;
Note
BETWEEN operator is inclusive, i.e., both the lower and upper limit values of the range are included.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 2/4
23/09/2021, 18:00 Revolutionizing the Job Market | NxtWave
Example
Output
Possible Mistakes
1. When using the BETWEEN operator, the first value should be less than second value. If not, we'll get an incorrect result
depending on the DBMS.
SQL
1 SELECT
2 name,
3 price,
4 brand
5 FROM
6 product
7 WHERE
8 price BETWEEN 500
9 AND 300;
Output
2. We have to give both lower limit and upper limit while specifying range.
SQL
1 SELECT
2 name,
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 3/4
23/09/2021, 18:00 Revolutionizing the Job Market | NxtWave
3 price,
4 brand
5 FROM
6 product
7 WHERE
8 price BETWEEN
9 AND 300;
SQL
1 Error: near "AND": syntax error
3. The data type of the column for which we're using the BETWEEN operator must match with the data types of the lower
and upper limits.
SQL
1 SELECT
2 name,
3 price,
4 brand
5 FROM
6 product
7 WHERE
8 name BETWEEN 300
9 AND 500;
Output
Try it Yourself!
Get all the products from product table with rating greater than 4.3 and less than 4.8
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 4/4
23/09/2021, 18:01 Revolutionizing the Job Market | NxtWave
In any e-commerce application, users have the option of sorting the products based on price,
rating, etc. Also, for any product, users could know all the distinct brands available for the
product.
Let's learn how to retrieve such ordered results and unique data!
Database
The database contains a
product table that stores the data of products like name, category, price, brand and
rating. You can check the schema and data of product table in the code playground.
ORDER BY
We use
Syntax
SQL
1 SELECT
2 column1,
3 column2,
4 ..columnN
5 FROM
6 table_name [WHERE condition]
7 ORDER BY
8 column1 ASC / DESC,
9 cloumn2 ASC / DESC;
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 1/3
23/09/2021, 18:01 Revolutionizing the Job Market | NxtWave
Example
Output
Try it Yourself!
product table in the descending order of their rating and in the ascending order
of price .
DISTINCT
Syntax
SQL
1 SELECT
2 DISTINCT column1,
3 column2,..
4 columnN
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 2/3
23/09/2021, 18:01 Revolutionizing the Job Market | NxtWave
5 FROM
6 table_name
7 WHERE
8 [condition];
Example
Output
Brand
Absa
Apple
...
Try it Yourself!
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 3/3
23/09/2021, 18:01 Revolutionizing the Job Market | NxtWave
Pagination
E-commerce applications like Amazon or Flipkart hold millions of products. But, the user does not require all the available
products every time s/he accesses the application. Infact, fetching all the products takes too long and consumes huge amount
of data.
Using pagination, only a chunk of the data can be sent to the user based on their request. And, the next chunk of data can be
fetched only when the user asks for it.
Let's understand more about pagination concept using the following databse.
Database
The database contains a
product table that stores the data of products like name, category, price, brand and rating. You can check the
schema and data of product table in the code playground.
LIMIT
LIMIT clause is used to specify the number of rows(n) we would like to have in result.
Syntax
SQL
1 SELECT
2 column1,
3 column2,..
4 columnN
5 FROM
6 table_name
7 LIMIT n;
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 1/4
23/09/2021, 18:01 Revolutionizing the Job Market | NxtWave
5 FROM
6 product
7 WHERE
8 brand = "Puma"
9 ORDER BY
10 rating DESC
Expand
Output
Try it Yourself!
Note
If the limit value is greater than the total number of rows, then all rows will be retrieved.
OFFSET
OFFSET clause is used to specify the position (from nth row) from where the chunk of the results are to be
selected.
Syntax
SQL
1 SELECT
2 column1,
3 column2,..
4 columnN
5 FROM
6 table_name
7 OFFSET n;
Example
Output
Possible Mistakes
SQL
1 Error: near "2": syntax error
SQL
1 Error: near "2": syntax error
Note
OFFSET clause should be placed after the LIMIT clause. Default OFFSET value is 0.
Try it Yourself!
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 3/4
23/09/2021, 18:01 Revolutionizing the Job Market | NxtWave
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=f01475e3-2c43-465e-a958-a5e75743… 4/4
23/09/2021, 18:02 Revolutionizing the Job Market | NxtWave
Aggregations
Consider the case of sports tournaments like cricket. Players’ performances are analysed based on their batting average,
maximum number of sixes hit, the least score in a tournament, etc.
We perform aggregations in such scenarios to combine multiple values into a single value, i.e., individual scores to an average
score.
Let’s learn more about aggregations to perform insightful analysis using the following database.
Database
The database consists of a player_match_details table that stores the information of players' details in a match like name,
match, score, year, number of fours and sixes scored.
Schema
SQL
1 player_match_details (
2 name VARCHAR(250),
3 match VARCHAR(10),
4 score INTEGER,
5 fours INTEGER,
6 sixes INTEGER,
7 year INTEGER
8 );
Aggregation Functions
Combining multiple values into a single value is called aggregation. Following are the functions provided by SQL to perform
aggregations on the given data:
Syntax
SQL
1 SELECT
2 aggregate_function(c1),
3 aggregate function(c2)
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 1/5
23/09/2021, 18:02 Revolutionizing the Job Market | NxtWave
gg g _ ( )
4 FROM
5 TABLE;
Note
Examples
1. Get the total runs scored by "Ram" from the player_match_details table.
SQL
1 SELECT
2 SUM(score)
3 FROM
4 player_match_details
5 WHERE
6 name = "Ram";
Output
SUM(score)
221
2. Get the highest and least scores among all the matches that happened in the year 2011.
SQL
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 2/5
23/09/2021, 18:02 Revolutionizing the Job Market | NxtWave
SQL
1 SELECT
2 MAX(score),
3 MIN(score)
4 FROM
5 player_match_details
6 WHERE
7 year = 2011;
Output
MAX(score) MIN(score)
75 62
COUNT Variants
Variant 1
SQL
1 SELECT COUNT(*)
2 FROM player_match_details;
Variant 2
SQL
1 SELECT COUNT(1)
2 FROM player_match_details;
Variant 3
SQL
1 SELECT COUNT()
2 FROM player_match_details;
Special Cases
When SUM function is applied on non-numeric data types like strings, date, time, datetime etc., SQLite DBMS
returns 0.0 and PostgreSQL DBMS returns None .
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 3/5
23/09/2021, 18:02 Revolutionizing the Job Market | NxtWave
NULL values:
MIN NULL
MAX NULL
SUM NULL
COUNT 0
AVG NULL
Alias
Syntax
SQL
1 SELECT
2 c1 AS a1,
3 c2 AS a2,
4 ...
5 FROM
6 table_name;
Examples
4 player_match_details;
Output
player_name
Ram
Joseph
---
Output
avg_score
60
Try it Yourself!
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 5/5
23/09/2021, 18:03 Revolutionizing the Job Market | NxtWave
Previously, we have learnt to perform aggregations on all the rows of a table. Now, we shall look at how to split a table into
multiple groups and apply aggregation on each group.
The GROUP BY keyword in SQL is used to group rows which have the same values for the mentioned attributes. You can
perform aggregations on these groups to get finer analytics.
HAVING keyword is used to further refine the data by filtering the aggregated values. Let’s explore more about GROUP BY
and HAVING clauses with the following database.
Database
player_match_details table which stores name, match, score, year, number of fours and sixes scored.
Schema
SQL
1 CREATE TABLE player_match_details (
2 name VARCHAR(250),
3 match VARCHAR(250),
4 score INTEGER,
5 fours INTEGER,
6 sixes INTEGER,
7 year INTEGER
8 );
GROUP BY
The
GROUP BY clause in SQL is used to group rows which have same values for the mentioned attributes.
Syntax
SQL
1 SELECT
2 c1,
3 aggregate_function(c2)
4 FROM
5 table_name
6 GROUP BY c1;
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 1/5
23/09/2021, 18:03 Revolutionizing the Job Market | NxtWave
SQL
1 SELECT
2 name, SUM(score) as total_score
3 FROM
4 player_match_details
5 GROUP BY name;
Output
name total_score
David 105
Joseph 116
Lokesh 186
... ...
Try it Yourself!
Syntax
SQL
1 SELECT
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 2/5
23/09/2021, 18:03 Revolutionizing the Job Market | NxtWave
1 SELECT
2 c1,
3 aggregate_function(c2)
4 FROM
5 table_name
6 WHERE
7 c3 = v1
8 GROUP BY c1;
Example
SQL
1 SELECT
2 name, COUNT(*) AS half_centuries
3 FROM
4 player_match_details
5 WHERE score >= 50
6 GROUP BY name;
Output
name half_centuries
David 1
Joseph 2
Lokesh 3
... ...
Try it Yourself!
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 3/5
23/09/2021, 18:03 Revolutionizing the Job Market | NxtWave
HAVING
HAVING clause is used to filter the resultant rows after the application of GROUP BY clause.
Syntax
SQL
1 SELECT
2 c1,
3 c2,
4 aggregate_function(c1)
5 FROM
6 table_name
7 GROUP BY
8 c1, c2
9 HAVING
10 condition;
Example
Get the
name and number of half_centuries of players who scored more than one half century.
SQL
1 SELECT
2 name,
3 count(*) AS half_centuries
4 FROM
5 player_match_details
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 4/5
23/09/2021, 18:03 Revolutionizing the Job Market | NxtWave
6 WHERE
7 score >= 50
8 GROUP BY
9 name
10 HAVING
Expand
Output
name half_centuries
Lokesh 2
Ram 3
Try it Yourself!
Get the name and number of half-centuries scored by each player who scored at least a half-century in two matches.
Note
WHERE vs HAVING: WHERE is used to filter rows and this operation is performed before grouping. HAVING is used to filter groups and
this operation is performed after grouping.
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=2f955c88-c100-431c-9d1e-6f6f2bd61… 5/5
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
Expressions in Querying
We can write expressions in various SQL clauses. Expressions can comprise of various data types like integers, floats, strings,
datetime, etc.
Database
Output
id name profit
2 Inception 67.68
N t
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 1/4
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
Note
2. Get the movie name and genre in the following format: movie_name - genre .
SQL
1 SELECT
2 name || " - " || genre AS movie_genre
3 FROM
4 movie;
Output
movie_genre
Inception - Action
...
Output
The Dark
3 Action 18.0 100.5 9.0
Knight
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 2/4
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
You can check the updation of movie ratings by retriving the data from the table.
Get all the genres that have average profit greater than 100 crores.
SQL
1 SELECT
2 genre
3 FROM
4 movie
5 GROUP BY
6 genre
7 HAVING
8 AVG(collection_in_cr - budget_in_cr) >= 100;
Output
genre
Action
Animation
Mystery
...
Try it Yourself!
Question 1
Get the profit of every movie with
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 3/4
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
Expected Output
The Dark
3 Drama 18 100.5 9
Knight
Question 2
Get all the movies having a
Expected Result
The Dark
3 Drama 18 100.5 9
Knight
Question 3
Scale up the ratings from 5 to 100 in the movie table.
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 4/4
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
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
SQL
1 strftime(format, field_name)
Example
Get the movie title and release year for every movie in the database
SQL
1 SELECT name, strftime('%Y', release_date)
2 FROM
3 movie;
2021-02-28 08:30:05
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 1/7
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
%H Hour 00 - 24 08
%m Month 01 - 12 02
%M Minute 00-59 30
Example
Get the number of movies released in each month of the year 2010
SQL
1 SELECT
2 strftime('%m', release_date) as month,
3 COUNT(*) as total_movies
4 FROM
5 movie
6 WHERE
7 strftime('%Y', release_date) = '2010'
8 GROUP BY
9 strftime('%m', release_date);
Output
month total_movies
03 2
05 1
06 3
.. ..
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
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 2/7
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
total_movies
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
month total_movies
06 6
CAST Function
Syntax
SQL
1 CAST(value AS data_type);
Example
Get the number of movies released in each month of the year 2010
SQL
1 SELECT strftime('%m', release_date) as month,
2 COUNT(*) as total_movies
3 FROM
4 movie
5 WHERE
6 CAST(strftime('%Y', release_date) AS INTEGER) = 2010
7 GROUP BY
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 3/7
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
8 strftime('%m', release_date);
Output
month total_movies
03 2
05 1
06 3
.. ..
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
Expected Output
year
1972
2008
2016
FLOOR Rounds a number to the nearest integer below its current value
CEIL Rounds a number to the nearest integer above its current value
You can refer the following table to further understand how floor, ceil and round work in general.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 4/7
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
FLOOR 2 3 4 5
CEIL 3 4 4 6
ROUND 2 4 4 6
Examples
1. Fetch the ceil, floor and round (to 1 decimal) values of the collections of all movies.
SQL
1 SELECT
2 name,
3 ROUND(collection_in_cr, 1) AS RoundedValue,
4 CEIL(collection_in_cr) AS CeilValue,
5 FLOOR(collection_in_cr) AS FloorValue
6 FROM
7 movie;
Output
Inception 83.7 84 83
String Functions
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.
SQL
1 SELECT
2 name
3 FROM
4 movie
5 WHERE UPPER(name) LIKE UPPER("%avengers%");
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 5/7
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
Output
name
Avengers: Endgame
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
Inception 16 16 16
Question 2
Get all the movie names that are released in 2010 and belong to "Action" genre.
Note:
Expected Output
movie_name
Inception
Iron Man 2
Thor
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 6/7
23/09/2021, 18:04 Revolutionizing the Job Market | NxtWave
movie_name
Spider-Man: Homecoming
...
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 7/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
CASE Clause
SQL provides CASE clause to perform conditional operations. This is similar to the switch case / if-else conditions in other
programming languages.
Let's learn more about the usage of CASE clause using the given database
Database
The IMDb dataset which consists of movies, actors and cast. You can refer to the database in the code playground for a
better understanding.
CASE Clause
CASE clause is evaluated and results in corresponding value when the first condition is met.
Syntax
SQL
1 SELECT c1, c2
2 CASE
3 WHEN condition1 THEN value1
4 WHEN condition2 THEN value2
5 ...
6 ELSE value
7 END AS cn
8 FROM table;
Note
1. In CASE clause, if no condition is satisfied, it returns the value in the ELSE part. If we do not specify the ELSE
part,
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 1/3
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
2. We can use CASE in various clauses like SELECT, WHERE, HAVING, ORDER BY and GROUP BY.
Example
Calculate the tax amount for all movies based on the profit. Check the following table for tax percentages.
profit tax_percentage
SQL
1 SELECT id, name,
2 CASE
3 WHEN collection_in_cr - budget_in_cr <= 100 THEN collection_in_cr - budget_in_cr * 0.1
4 WHEN (collection_in_cr - budget_in_cr > 100
5 AND collection_in_cr - budget_in_cr < 500) THEN collection_in_cr - budget_in_cr * 0.15
6 ELSE collection_in_cr - budget_in_cr * 0.18
7 END AS tax_amount
8 FROM
9 movie;
Output
id name tax_amount
2 Inception 82.08
Try it Yourself
Question 1
rating category
<5 Poor
7< Good
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 2/3
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
Example
Get the number of movies with rating greater than or equal to 8, and the movies with rating less than 8, and are released
between 2015 and 2020.
SQL
1 SELECT
2 count(
3 CASE
4 WHEN rating >= 8 THEN 1
5 END
6 ) AS above_eight,
7 count(
8 CASE
9 WHEN rating < 8 THEN 1
10 END
Expand
Output
above_eight below_eight
4 2
Try it Yourself!
Get the number of movies with collection greater than or equal to 100 crores, and the movies with collection less than 100
crores.
Output
above_100_cr below_100_cr
13 7
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 3/3
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
The SQL Set operation is used to combine the two or more SQL queries.
Let us understand common set operators by performing operations on two sets
INTERSECT
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 1/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
MINUS
UNION
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 2/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
Result: Jude Law, Mark Strong, Robert D Jr, Chris Evans, Mark Ruffalo
UNION ALL
Result: Jude Law, Mark Strong, Robert D Jr, Robert D Jr, Chris Evans, Mark Ruffalo
Let's learn more about CASE clause using the given database
Database
The IMDb dataset which consists of movies, actors and cast. You can refer to the database in the code playground for a
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 3/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
better understanding.
We can apply these set operations on the one or more sql queries to combine their results
Syntax
SQL
1 SELECT
2 c1, c2
3 FROM
4 table_name
5 SET_OPERATOR
6 SELECT
7 c1, c2
8 FROM
9 table_name;
Basic rules when combining two sql queries using set operations
Examples
Get ids of actors who acted in both Sherlock Holmes(id=6) and Avengers Endgame(id=15)?
SQL
1 SELECT actor_id
2 FROM cast
3 WHERE movie_id=6
4 INTERSECT
5 SELECT actor_id
6 FROM cast
7 WHERE movie_id=15;
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 4/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
Output
actor_id
Get ids of actors who acted in Sherlock Holmes(id=6) and not in Avengers Endgame(id=15)?
SQL
1 SELECT actor_id
2 FROM cast
3 WHERE movie_id=6
4 EXCEPT
5 SELECT actor_id
6 FROM cast
7 WHERE movie_id=15;
Output
actor_id
16
21
Get distinct ids of actors who acted in Sherlock Holmes(id=6) or Avengers Endgame(id=15).
SQL
1 SELECT actor_id
2 FROM cast
3 WHERE movie_id=6
4 UNION
5 SELECT actor_id
6 FROM cast
7 WHERE movie_id=15;
Output
actor_id
16
21
22
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 5/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
1 SELECT actor_id
2 FROM cast
3 WHERE movie_id=6
4 UNION ALL
5 SELECT actor_id
6 FROM cast
7 WHERE movie_id=15;
Output
actor_id
16
21
22
Try it Yourself!
1. Get all the movie ids in which actors Robert Downey Jr. (id=6) & Chris Evans(id=22) have been casted
2. Get all the movie ids in which actor Robert Downey Jr. (id=6) is casted and not Chris Evans(id=22)
3. Get all the unique movie ids in which either actor Robert Downey Jr. (id=6) or Chris Evans(id=22) is casted
ORDER BY clause can appear only once at the end of the query containing multiple SELECT statements.
While using Set Operators, individual SELECT statements cannot have ORDER BY clause. Additionally, sorting can be done
based on the columns that appear in the first SELECT query. For this reason, it is recommended to sort this kind of queries
using column positions.
Example
Get distinct ids of actors who acted in Sherlock Holmes (id=6) or Avengers Endgame(id=15). Sort ids in the descending order.
SQL
1 SELECT actor_id
2 FROM cast
3 WHERE movie_id=6
4 UNION
5 SELECT actor_id
6 FROM cast
7 WHERE movie_id=15
8 ORDER BY 1 DESC;
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 6/7
23/09/2021, 18:05 Revolutionizing the Job Market | NxtWave
Try it Yourself!
1. Get all the movie ids in which actor Robert Downey Jr. (id=6) is casted & not Chris Evans(id=22). Sort the ids in the
descending order.
Similar to ORDER BY clause, LIMIT and OFFSET clauses are used at the end of the list of queries.
Example
Get the first 5 ids of actors who acted in Sherlock Holmes (id=6) or Avengers Endgame(id=15). Sort ids in the descending
order.
SQL
1 SELECT actor_id
2 FROM cast
3 WHERE movie_id=6
4 UNION
5 SELECT actor_id
6 FROM cast
7 WHERE movie_id=15
8 ORDER BY 1 DESC
9 LIMIT 5;
Try it Yourself!
1. Get the first 5 unique movie ids in which either actor Robert Downey Jr. (id=6) or Ryan Reynolds(id=7) is casted. Sort ids
in the descending order.
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=b3ba6586-c110-463d-bb75-95562000… 7/7
23/09/2021, 18:06 Revolutionizing the Job Market | NxtWave
Clauses
SELECT SELECT * FROM ... Retrieves all the columns from a table
DELETE DELETE FROM table_name Deletes all the rows from the table
DROP DROP TABLE table_name Deletes the table from the database
DISTINCT SELECT DISTINCT col, ... Gets the unique values of given column(s)
Operators
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=04c57287-8217-49b6-9d29-905ca8e6… 1/3
23/09/2021, 18:06 Revolutionizing the Job Market | NxtWave
Functions
SELECT
COUNT COUNT(col) Counts the number of values in the given column
...
SELECT
SUM Adds all the values of given column
SUM(col) ...
SELECT
MIN Gets the minimum value of given column
MIN(col) ...
SELECT
MAX Gets the maximum value of given column
MAX(col) ...
SELECT Gets the average of the values present in the given
AVG
AVG(col) ... column
Extracts the year from the column value in string format.
strftime("%Y",
strftime() Similarly, we can extract month, day, week of the day and
col) ...
many.
CAST(col AS
CAST() Converts the value to the given datatype
datatype) ...
Rounds a number to the nearest integer below its
FLOOR() FLOOR(col)
current value
Rounds a number to the nearest integer above its
CEIL() CEIL (col)
current value
Rounds a number to a specified number of decimal
ROUND() ROUND(col)
places
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=04c57287-8217-49b6-9d29-905ca8e6… 2/3
23/09/2021, 18:06 Revolutionizing the Job Market | NxtWave
MARKED AS COMPLETE
Submit Feedback
Notes
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=04c57287-8217-49b6-9d29-905ca8e6… 3/3
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
To model a database, we have to first understand the business requirements at conceptual level, which is later
translated into a relational database.
Entity
Attributes of an Entity
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 1/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
Key Attribute
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 2/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
The attribute that uniquely identifies each entity is called key attribute.
Entity Type
Entity Type is a collection of entities that have the same attributes (not values).
Relationships
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 3/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
Example:
Each student can register for many courses, and a course can have many students.
Types of relationships
One-to-One Relationship
One-to-Many or Many-to-One Relationship
Many-to-Many Relationship
One-to-One Relationship
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 4/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
One-to-Many Relationship
Example
A person can have many cars. But a car belongs to only one person.
Many-to-Many Relationship
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 5/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
Example
Cardinality Ratio
Cardinality in DBMS defines the maximum number of times an instance in one entity can relate to instances of
another entity.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 6/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
MARKED AS COMPLETE
Submit Feedback
Notes
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 7/7
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
In the previous cheatsheet, we have understood the core concepts of ER Model — entity types,
relationships and attributes. Now, let's build an ER model for a real-world scenario.
E-commerce Application
Customer has only one cart. A cart belongs to only one customer
Customer can save multiple addresses in the application for further use like selecting delivery address
Entity types
Customer
Product
Cart
Address
Relationships
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 1/4
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
Hence, the relation between customer and cart entities is One-to-One relation.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 2/4
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
Attributes
Following are the attributes for the entity types in the e-commerce scenario.
Here, attributes like id, product_id, etc., are key attributes as they uniquely identify each entity in the
entity type.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 3/4
23/09/2021, 18:07 Revolutionizing the Job Market | NxtWave
Submit Feedback
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 4/4
23/09/2021, 18:08 Revolutionizing the Job Market | NxtWave
In the previous cheatsheet, we've learnt to build an ER model for a given scenario. Now, let's convert this ER model
to Relational Database.
Let's consider the same e-commerce application.
E-commerce Application
Customer has only one cart. A cart belongs to only one customer
Customer can save multiple addresses in the application for further use like selecting delivery address
Primary key: A minimal set of attributes (columns) in a table that uniquely identifies rows in a table.
In the following tables, all the ids are primary keys as they uniquely identify each row in the table.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 1/5
23/09/2021, 18:08 Revolutionizing the Job Market | NxtWave
Relationships
We store the primary key of a customer in the address table to denote that the addresses are related to a
particular customer.
This new column/s in the table that refer to the primary key of another table is called Foreign Key.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 2/5
23/09/2021, 18:08 Revolutionizing the Job Market | NxtWave
Here,
This is similar to one-to-many relationship. But, we need to ensure that only one cart is associated to a customer
Here, we cannot store either the primary key of a product in the cart table or vice versa.
To store the relationship between the cart and product tables, we use a Junction Table.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 3/5
23/09/2021, 18:08 Revolutionizing the Job Market | NxtWave
Note
We store the properties of a the relationship in the junction table. For example, quantity of each product in
the cart should be stored in the junction table cart_product
ER Model
Relational Database
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 4/5
23/09/2021, 18:08 Revolutionizing the Job Market | NxtWave
Submit Feedback
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 5/5
23/09/2021, 18:09 Revolutionizing the Job Market | NxtWave
In the previous sessions, we've explored how to represent an ER model in the form of tables in a relational database.
Now, let's create tables to store the data in the database by defining all the columns and relationships between the tables.
Consider the e-commerce scenario. The tables, columns and the relations between them are s follows.
Primary Key
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 1/4
23/09/2021, 18:09 Revolutionizing the Job Market | NxtWave
Syntax
SQL
1 CREATE TABLE table_name (
2 c1 t1 NOT NULL PRIMARY KEY,
3 ...
4 cn tn,
5 );
Foreign Key
Syntax
SQL
1 CREATE TABLE table2(
2 c1 t1 NOT NULL PRIMARY KEY,
3 c2 t2,
4 FOREIGN KEY(c2) REFERENCES table1(c3) ON DELETE CASCADE
5 );
Understanding
SQL
1 FOREIGN KEY(c2) REFERENCES table1(c3)
Above part of the foreign key constraint ensure that foreign key can only contain values that are in the referenced primary
key.
SQL
1 ON DELETE CASCADE
table1 is deleted, then all its related rows in table2 will also be deleted.
Note
To enable foreign key constraints in SQLite, use PRAGMA foreign_keys = ON; By default it is enabled in our platform!
Customer Table
SQL
1 CREATE TABLE customer (
2 id INTEGER NOT NULL PRIMARY KEY,
3 name VARCHAR(250),
4 INT
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 2/4
23/09/2021, 18:09 Revolutionizing the Job Market | NxtWave
4 age INT
5 );
Product Table
SQL
1 CREATE TABLE product (
2 id INTEGER NOT NULL PRIMARY KEY,
3 name VARCHAR(250),
4 price INT,
5 brand VARCHAR(250),
6 category VARCHAR(250)
7 );
Address Table
SQL
1 CREATE TABLE address(
2 id INTEGER NOT NULL PRIMARY KEY,
3 pin_code INTEGER,
4 door_no VARCHAR(250),
5 city VARCHAR(250),
6 customer_id INTEGER,
7 FOREIGN KEY (customer_id) REFERENCES customer(id) ON DELETE CASCADE
8 );
Cart Table
SQL
1 CREATE TABLE cart(
2 id INTEGER NOT NULL PRIMARY KEY,
3 customer_id INTEGER NOT NULL UNIQUE,
4 total_price INTEGER,
5 FOREIGN KEY (customer_id) REFERENCES customer(id) ON DELETE CASCADE
6 );
SQL
1 CREATE TABLE cart_product(
2 id INTEGER NOT NULL PRIMARY KEY,
3 cart_id INTEGER,
4 product_id INTEGER,
5 quantity INTEGER,
6 FOREIGN KEY (cart_id) REFERENCES cart(id) ON DELETE CASCADE,
7 FOREIGN KEY (product_id) REFERENCES product(id) ON DELETE CASCADE
8 );
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 3/4
23/09/2021, 18:09 Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d… 4/4
23/09/2021, 18:10 Revolutionizing the Job Market | NxtWave
JOINS
So far, we have learnt to analyse the data that is present in a single table. But in the real-world scenarios, often, the data is
distributed in multiple tables. To fetch meaningful insights, we have to bring the data together by combining the tables.
We use JOIN clause to combine rows from two or more tables, based on a related column between them. There are various
types of joins, namely Natural join, Inner Join, Full Join, Cross Join, Left join, Right join.
Database
Here, the database stores the data of students, courses, course reviews, instructors, etc., of an e-learning platform.
Refer the tables in the code playground for a better understanding of the database.
Natural JOIN
Syntax
SQL
1 SELECT *
2 FROM table1
3 NATURAL JOIN table2;
Example
Solving this problem involves querying on data stored in two tables, i.e.,
course & instructor . Both the tables have common column instructor_id . Hence, we use Natural Join.
SQL
1 SELECT course.name,
2 instructor.full_name
3 FROM course
4 NATURAL JOIN instructor
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 1/6
23/09/2021, 18:10 Revolutionizing the Job Market | NxtWave
Output
name full_name
Try it Yourself!
Question 1:
Get the details of the instructor who is teaching "Cyber Security".
Expected Output:
full_name gender
Alex M
Question 2:
Get student full name and their scores in "Machine Learning" (course with id=11).
Expected Output:
full_name score
Varun 80
Sandhya 90
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 2/6
23/09/2021, 18:10 Revolutionizing the Job Market | NxtWave
INNER JOIN
INNER JOIN combines rows from both the tables if they meet a specified condition.
Syntax
SQL
1 SELECT *
2 FROM table1
3 INNER JOIN table2
4 ON table1.c1 = table2.c2;
Note
Example
Output
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 3/6
23/09/2021, 18:10 Revolutionizing the Job Market | NxtWave
Try it Yourself!
Question 1:
Get the details of students who enrolled for "Machine Learning" (course with id=11).
Expected Output:
Varun 16 M
Sandhya 19 F
Question 2:
Get the reviews given by "Varun" (student with id = 1)
Expected Output:
LEFT JOIN
In
, for each row in the left table, matched rows from the right table are combined. If there is no
LEFT JOIN
match, NULL values are assigned to the right half of the rows in the temporary table.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 4/6
23/09/2021, 18:10 Revolutionizing the Job Market | NxtWave
Syntax
SQL
1 SELECT *
2 FROM table1
3 LEFT JOIN table2
4 ON table1.c1 = tabl2.c2;
Example
Fetch the full_name of students who have not enrolled for any course
SQL
1 SELECT student.full_name
2 FROM student
3 LEFT JOIN student_course
4 ON student.id = student_course.student_id
5 WHERE student_course.id IS NULL;
Output
full_name
Afrin
Try it Yourself!
Question 1:
Get the course details that doesn't have any students.
Expected Output:
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 5/6
23/09/2021, 18:10 Revolutionizing the Job Market | NxtWave
name
Linux
Question 2:
Get the instructors details who is not assigned for any course.
Expected Output:
full_name gender
Bentlee M
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 6/6
23/09/2021, 18:11 Revolutionizing the Job Market | NxtWave
Database
Here, the database stores the data of students, courses, course reviews, instructors, etc., of an e-learning platform.
Refer the tables in the code playground for a better understanding of the database.
Example
Fetch all the students who enrolled for the courses taught by the instructor “Arun” (id = 102)
SQL
1 SELECT T.name AS course_name,
2 student.full_name
3 FROM (course
4 INNER JOIN student_course
5 ON course.id = student_course.course_id) AS T
6 INNER JOIN student
7 ON T.student_id = student.id
8 WHERE course.instructor_id = 102;
Output
course_name full_name
Note
Best Practices
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 1/4
23/09/2021, 18:11 Revolutionizing the Job Market | NxtWave
1. Use
2. Use alias table names to refer the columns in the combined table.
Try it Yourself!
Question 1:
Fetch the name of the students who gave reviews to the "Machine Learning" course.
Expected Output:
full_name
Varun
Question 2:
Expected Output:
course_name
Machine Learning
Let's learn about the Right Join, Full Join and Cross Join in the upcoming cheatsheet.
We can apply
WHERE , ORDER BY , HAVING , GROUP BY , LIMIT , OFFSET and other clauses (which are used for
retrieving data tables) on the temporary joined table as well.
Example:
Get the name of the student who scored highest in "Machine Learning" course.
SQL
1 SELECT student.full_name
2 FROM (course
3 INNER JOIN student_course
4 ON course.id = student_course.course_id) AS T
5 INNER JOIN student
6 ON T.student_id = student.id
7 WHERE course.name = "Machine Learning"
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 2/4
23/09/2021, 18:11 Revolutionizing the Job Market | NxtWave
g
8 ORDER BY student_course.score DESC
9 LIMIT 1;
Output
full_name
Sandhya
Try it Yourself!
Question 1:
Get all the courses taken by the student with id=1 and his respective scores in each course
Expected Output
name score
Machine learning 80
Question 2:
Get all the students who registered for at least one course.
Expected Output
full_name
Varun
Ajay
Sandhya
We can apply
WHERE , ORDER BY , HAVING , GROUP BY , LIMIT , OFFSET and other clauses (which are used for
retrieving data tables) on the temporary joined table as well.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 3/4
23/09/2021, 18:11 Revolutionizing the Job Market | NxtWave
6 LEFT JOIN student_course
7 ON course.id = student_course.course_id
8 GROUP BY
9 course.id;
Output
course_name highest_score
Machine Learning 90
Cyber Security 60
Linux
Try it Yourself!
Question 1:
Get the course name and the average score for each course.
Expected Output
name avg_score
Machine Learning 85
Cyber Security 60
Linux
Question 2:
Get the number of students in each course .
Expected Output
name no_of_students
Machine learning 2
Cyber Security 1
linux 0
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 4/4
23/09/2021, 18:11 Revolutionizing the Job Market | NxtWave
JOINS Cont'd
Database
The database stores the data of students, courses, course reviews, instructors, etc., of an e-learning platform.
Refer the tables in the code palyground for a better understanding of the database.
RIGHT JOIN
, for each row in the right table, matched rows from the left table are combined. If there is no
RIGHT JOIN
match, NULL values are assigned to the left half of the rows in the temporary table.
Syntax
SQL
1 SELECT *
2 FROM table1
3 RIGHT JOIN table2
4 ON table1.c1 = table2.c2;
Which is similar to
SQL
1 SELECT *
2 FROM table2
3 LEFT JOIN table1
4 ON table1.c1 = table2.c2;
Example
Note
FULL JOIN
FULL JOIN or FULL OUTER JOIN is the result of both RIGHT JOIN and LEFT JOIN
Syntax
SQL
1 SELECT *
2 FROM table1
3 FULL JOIN table2
4 ON c1 = c2;
Example
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 2/5
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
Note
CROSS JOIN
In CROSS JOIN, each row from the first table is combined with all rows in the second table.
Cross Join is also called as CARTESIAN JOIN
Syntax
SQL
1 SELECT *
2 FROM table1
3 CROSS JOIN table2;
Example
SQL
1 SELECT course.name AS course_name,
2 instructor.full_name AS instructor_name
3 FROM course
4 CROSS JOIN instructor;
Output
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 3/5
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
course_name instructor_name
... ...
SELF JOIN
So far, we have learnt to combine different tables. We can also combine a table with itself. This kind of join is called SELF-
JOIN.
Syntax
SQL
1 SELECT t1.c1,
2 t2.c2
3 FROM table1 AS t1
4 JOIN table1 AS t2
5 ON t1.c1 = t2.cn;
Note
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 4/5
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
Example
Output
1 3 11
JOINS Summary
Left Join All rows from left table & matched rows from right table
Right Join All rows from right table & matched rows from left table
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=10f3582f-5069-41de-ad90-961b27901… 5/5
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
Views
Database:
The database stores the sample data of an e-commerce aplication.
Here, the database consists of
user , order_details and product tables that store the information of products, orders placed, and the products
on the platform.
Refer the tables in the code playground for a better understanding of the database.
View
Create View
Example
Create
Note
We cannot perform write operations like updating, deleting & inserting rows in the base tables through views.
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 1/4
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
Try it Yourself!
Create
order_with_products view with order_id, product_id, no_of_units, name, price_per_unit, rating, category, brand.
We can use its name instead of writing the original query to get the data.
SQL
1 SELECT *
2 FROM user_base_details;
Output
We can use same operations which are used on tables like WHERE clause, Ordering results, etc.
error .
Example
SQL
1 SELECT name, address
2 FROM user_base_details
3 WHERE gender = "Male";
4 ORDER BY age ASC;
Output
SQL
1 Error: no such column:address
Try it Yourself!
From the
order_with_products view created above, get the name and no_of_units ordered in order_id = 802.
Expected Output
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 2/4
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
name no_of_units
Oneplus 8 Pro 1
Gorilla Glass 1
In SQLite, to list all the available views, we use the folowing query.
SQL
1 SELECT
2 name
3 FROM
4 sqlite_master
5 WHERE
6 TYPE = 'view';
Output
name
order_with_products
user_base_details
Delete View
Syntax
SQL
1 DROP VIEW view_name;
Example
Delete
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 3/4
23/09/2021, 18:12 Revolutionizing the Job Market | NxtWave
Advantages
Views are used to write complex queries that involves multiple joins, group by, etc., and can be used whenever needed.
Restrict access to the data such that a user can only see limited data instead of a complete table.
MARKED AS COMPLET
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 4/4
23/09/2021, 18:13 Revolutionizing the Job Market | NxtWave
Subqueries
Database:
The database stores the sample data of an e-commerce aplication.
Here, the database consists of
user , order_details and product tables that store the information of products, orders placed, and the products
on the platform.
Refer the tables in the code playground for a better understanding of the database.
Examples
Example 1:
Get the rating variance of products in the "WATCH" category. Rating variance is the difference between average rating and
rating of a product.
Here, we need to write an expression to subtract rating of each product from the average rating as following.
SQL
1 SELECT name,
2 (average_rating - rating) AS rating_variance
3 ...
Output
name rating_variance
Analog-Digital -0.766666666666667
... ...
Example 2:
Fetch all the products whose ratings is greater than average rating of all products.
Output
SQL
1
2 SELECT *
3 FROM product
4 WHERE rating > (
5 SELECT AVG(rating)
6 FROM product
7 );
Expected Output
Biotique
202 34 4.5 SOAP BIPTIQUE
Almond Soap
Boat Stone
203 1999 4.3 SPEAKER BOAT
Speaker
Example 3:
Fetch all the order_ids in which order consists of mobile (product_ids : 291, 292, 293, 294, 296) and not ear phones
(product_ids : 227, 228, 229, 232, 233).
SQL
1 SELECT
2 order_id
3 FROM
4 order_details
5 WHERE
6 order_id IN (
7 SELECT
8 order_id
9 FROM
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 2/4
23/09/2021, 18:13 Revolutionizing the Job Market | NxtWave
10 order_product
Expand
Output
order_id
801
802
806
807
Possible Mistakes
In SELECT Clause
Query
SQL
1 SELECT name, (
2 SELECT AVG(rating), MAX(rating)
3 FROM product
4 WHERE category = "WATCH"
5 ) - rating AS rating_variance
6 FROM product
7 WHERE category = "WATCH";
Output
SQL
1 Error:
2 sub-select returns 2 columns - expected 1
In WHERE Clause
Query
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 3/4
23/09/2021, 18:13 Revolutionizing the Job Market | NxtWave
Output
SQL
1 Error: Row value misused
Try it Yourself!
Question 1
Get the rating variance of products in the "MOBILE" category. Rating variance is the difference between average rating and
rating of a product.
Rating variance is the difference between average rating and rating of a product
name rating_variance
... ...
Question 2
Get all the products from the "MOBILE" category, where rating is greater than average rating.
name rating
Mi 10T 4.5
... ...
MARKED AS COMPLETE
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=a5f334cf-2c64-40bd-a528-16829629c… 4/4
23/09/2021, 18:14 Revolutionizing the Job Market | NxtWave
Transactions
Transactions are used in various scenarios such as banking, ecommerce, social networks, booking tickets,
etc.
Atomicity
Consistency
Isolation
Durability
Atomicity
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=69c58008-5e8f-4256-bbef-b928043e2… 1/4
23/09/2021, 18:14 Revolutionizing the Job Market | NxtWave
Consistency
Isolation
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=69c58008-5e8f-4256-bbef-b928043e2… 2/4
23/09/2021, 18:14 Revolutionizing the Job Market | NxtWave
Multiple transaction can occur at the same time without adversely affecting the other.
Durability
Indexes
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=69c58008-5e8f-4256-bbef-b928043e2… 3/4
23/09/2021, 18:14 Revolutionizing the Job Market | NxtWave
In scenarios like, searching for a word in dictionary, we use index to easily search for the word. Similarly,
in databases, we maintain indexes to speed up the search for data in a table.
Submit Feedback
https://learning.ccbp.in/backend-development/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=69c58008-5e8f-4256-bbef-b928043e2… 4/4