You are on page 1of 49

ASSIGNMENT

DBMS
Submitted To:
Sir Faizan Ahmad
Submitted By:
Bushra Munawar (2019-CS-622)
Rabia Parveen (2019-CS-602)
Soha Noor (2019-CS-614)
CLASS:
DBMS LAB
SEMESTER:
4th
SECTION:
A
Department of Computer Science, University of Engineering
and Technology, New Main Campus Lahore

Movie Database
ABSTRACT:
 SQL stands for Structured Query Language and it is an ANSI standard computer
language for accessing and manipulating database systems. It is used for managing data
in relational database management system which stores data in the form of tables and
relationship between data is also stored in the form of tables. SQL statements are used to
retrieve and update data in a database. Keeping this in mind, we created a Movie
Database and implemented some of the commands which include DML Commands,
DDL Commands, SQL Commands, SQL Joins, Boolean, Relational & Arithmetic
Operators etc. The description of each Query along with their proper syntax and results
is provided.
 Here we made database of movie having 4 tables named as movie table, actor
table,rating table and reviwer table. Movie_id is primery key.and use as forign key in
rating id.and similarly reviewer id is also use as foreign key in rating table while
reviwer id iself a unique key.

Table Created & Used in Movie Database:


Movie_Table
Movie_ID Movie_Title Release_Date Movie_Language Country
A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Actor_Table
Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link
AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB8 Daniel Radcliff Male Rad@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom

Rating_Table
Movie_ID Rev_ID Rev_Stars
A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16
Pictorial Representation of Movie Database:

DML Commands:

1) Update:
We will use Update key word to change any value in the table. In the following example we will
change the Rev_Stars of movie_id B13 FROM 3.5 TO 4.5.
 Lets see the original table.

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
UPDATE Rating Table
SET Rev_Stars = 4.5
WHERE movie_id = B13;
 After applying above query resulted table is

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 4.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

2) DELETE:
We will use DELETE query to remove some data from table. We can also remove whole
table.
 Lets see the original table named as Actor _Table.
Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link
AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB8 Daniel Radcliff Male Rad@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom

Syntax:
DELETE FROM Actor_Table
WHERE Actor_FName = 'Daniel';
 After applying query resulted table is:

Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link


AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom

3) INSERT INTO:
The INSERT INTO statement is used to insert new records in a table.
Syntax:

It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table.

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
Example:
Insert into Reviewer_Table
Values (12,’Chris’,20),
(13,’Amir’,30),
(14,’Raj’,25);

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25

DDL Commands:
4)CREATE:

This command creates table. But here we have to give attributes and data types of attributes too.

Syntax:
Create Table TableName
(attributes);

Example:
Create Table Movie_table
(Movie_ID text not null primary key,
Movie_Title varchar(50),
Release_Date date,
Movie_Language varchar(20),
Country char(20));
Movie_ID Movie_Title Release_Date Movie_Language Country
A table like this will be created.

5) ALTER:
Use Alter command to change the data type of attributes. As well as it is use to add, delete ,drop
specific column from table.in the following example we’ll see how we’ll drop column from
table.
 Lets see the original table named as Actor _Table.

Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link


AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom

Syntax:
ALTER TABLE Actor_Table
DROP COLUMN Contact_link;
 After applying query resulted table is following.

Actor_ID Actor_FName Actor_LName Actor_Gender


AB1 Tom Cruise Male
AB2 Raj Kumar Male
AB3 Jahnvi kapoor Female
AB4 Hanery Cavil Male
AB5 Millie Brown Female
AB6 Emma Watson Female
AB7 Scarlet Johanson Female
AB9 Robert Pattinson Male
AB10 Elizbeth Olsen Female
AB11 Chris Evans Male
AB12 Loius Hoffman Male
AB13 Park Shinhye Female

6) Drop:
DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view. A DROP statement in SQL removes a
component from a relational database management system (RDBMS). If we want to drop a
column than we have to use drop command with Alter table Command.

Syntax:
DROP TABLE table_name;
Example:
Drop table Movie_Table;
This command will delete the whole Movie_Table from database.
7) COMMENTS:
Comments are used to explain SQL statements. It’ll not effect the execution of statements. For
single line comment use - - and for multiple line comment use /* */ .
 Lets see the original table named as reviewed_table.

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Syntax:
/*
It will sort the
table in Ascending
order w-r-t rev_id.
By default
*/
SELECT * FROM Reviewer_Table
ORDER BY Rev_ID ASC;
 After applying commnets we’ll see no changes will occure.

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view. A DROP statement in SQL removes a
component from a relational database management system (RDBMS). If we want to drop a
column than we have to use drop command with Alter table Command.

Syntax:
DROP TABLE table_name;
Example:
Drop table Movie_Table;
This command will delete the whole Movie_Table from database.
8). TRUNCATE TABLE
The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.

 Lets see the original table named as Actor _Table.

Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link


AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom
Syntax:

The following SQL truncates the

TRUNCATE TABLE Actor_Table;

DQL Commands:
9)SELECT *:
If you want to select all the fields available in the table.

SELECT * Syntax:

SELECT * FROM table_name;


Example:
SELECT * Reviewer_Table;

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
21 Bonnie 16

10)Select specific Column:


The SELECT statement is used to select data from a database.

SELECT specific column Syntax:


SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want to select data from.

Example:
SELECT Rev_ID, Rev_Name
FROM Reviewer_Table

Rev_ID Rev_Name
12 Chris
13 Amir
14 Raj
15 Soha
16 Rabia
17 Bushra
18 Usman
19 Robert
20 Caroline
21 Bonnie

Constraints in SQL:
The following constraints are commonly used in SQL:

 NOT NULL - Ensures that a column cannot have a NULL value


 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
 FOREIGN KEY - Prevents actions that would destroy links between tables
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified

11)Not Null:
By default, a column can hold NULL values. The NOT NULL constraint enforces a column
to not accept NULL values. This enforces a field to always contain a value, which means that
you cannot insert a new record, or update a record without adding a value to this field.
Example:

CREATE TABLE Rating_Table (


Movie_ID text NOT NULL,
Rev_ID int NOT NULL,
Rev_Stars decimal
);

12)Unique:
The UNIQUE constraint ensures that all values in a column are different. Both
the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has
a UNIQUE constraint.
Example:

CREATE TABLE Rating_Table (


Movie_ID text NOT NULL unique,
Rev_ID int NOT NULL unique,
Rev_Stars decimal
);

13)Primary key:
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must
contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary
key; and in the table, this primary key can consist of single or multiple columns.

Example:
Create Table Reviewer_Table(
Rev_ID int not null unique Primary Key,
Rev_Name Varchar(30),
Rev_Age int);

14) Foreign Key:

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY
KEY in another table. The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.

Example:
Here, Rating_Table uses primary Key (Rev_ID) of Reviewer table as Foreign Key for Relstion
between two tables.

CREATE TABLE Rating_Table (


Movie_ID text NOT NULL unique,
Rev_ID int NOT NULL FOREIGN Key References Review_Table(Rev_ID),
Rev_Stars decimal
);

15) Check:
The CHECK constraint is used to limit the value range that can be placed in a column. If you
define a CHECK constraint on a column it will allow only certain values for this column. If you
define a CHECK constraint on a table it can limit the values in certain columns based on values
in other columns in the row.

Example:

Create Table Reviewer_Table(


Rev_ID int not null unique Primary Key,
Rev_Name Varchar(30),
Rev_Age int,
Check (Rv_Age>15)
);
Here, if we insert age 15 or less than 15 it will cause error.
 Default:

The DEFAULT constraint is used to set a default value for a column. The default value will be
added to all new records, if no other value is specified.

Example:
Create Table Reviewer_Table(
Rev_ID int not null unique Primary Key,
Rev_Name Varchar(30),
Rev_Age int Default 20,
);
Here, if we don’t insert age then by default it will be 20.
SQL JOINS:
16). INNER JOIN:
A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.

 Let see the example.


There is a relationship between the "Orders" table and the
"Orders" table by way of the "CustomerID" column. We will use this relationship
to create the following SQL statement, which displays the two tables (by means of an
INNER JOIN).
Rating _Table

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Syntax:
SELECT Rating_Table.Rev_ID, Reviewer_Table.Mov_ID, Rating.Rev_Stars
FROM Rating_Table INNER JOIN
ON Reviewer_Table Rating_Table.Rev_ID = Reviewer_Table.Rev_ID;

 The result of above Query is:

Movie_ID Rev_Age Rev_Stars


A12 20 4
B13 30 3.5
C56 25 2.5
Z11 26 3.7
D7 22 4.8
F9 25 5
S7 35 3.2
M8 32 4.2
N57 18 4
X11 16 3.9

17). OUTER JOIN:


The OUTER JOIN keyword returns all matching records from both tables whether the other table
matches or not. So, if there are rows in "Customers" that do not have matches in "Orders", or if there
are rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.
FULL OUTER JOIN and FULL JOIN are the same.
Here are the tables from the movie database

Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Rating _Table

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT Reviewer_Table.Rev_Name, Rating_Table.Mov_ID
FROM Reviewer_Table Full Outer JOIN
ON Reviewer_Table.Rev_ID = Rating_Table.Rev_ID;
ORDER BY Reviewer_Table.Rev_Name;

 The result of above Query is:

Rev_Name Movie_ID
Chris A12
Amir B13
Raj C56
Soha Z11
Rabia D7
Bushra F9
Usman S7
Robert M8
Caroline N57
Bonnie X11
18). RIGHT JOIN:
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no match.

Here are the tables from the movie database

Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Rating _Table

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT Reviewer_Table.Rev_Name, Rating_Table.Mov_ID, Rating_Table.Rev_Stars
FROM Reviewer_Table
RIGHT JOIN Rating_Table
ON Reviewer_Table.Rev_ID = Rating_Table.Rev_ID;
ORDER BY Reviewer_Table.Rev_Name;

 The result of above Query is:

Rev_Name Movie_ID Rev_Stars


Chris A12 4
Amir B13 3.5
Raj C56 2.5
Soha Z11 3.7
Rabia D7 4.8
Bushra F9 5
Usman S7 3.2
Robert M8 4.2
Caroline N57 4
Bonnie X11 3.9

19). LEFT JOIN:


The LEFT JOIN keyword returns all records from the left table (table1), and the matching records
from the right table (table2). The result is 0 records from the right side, if there is no match.

Here are the tables from the movie database

Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Rating _Table

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT Reviewer_Table.Rev_Name, Rating_Table.Mov_ID
FROM Reviewer_Table
LEFT JOIN Rating_Table
ON Reviewer_Table.Rev_ID = Rating_Table.Rev_ID;
ORDER BY Reviewer_Table.Rev_Name;

 The result of above Query is:

Rev_Name Movie_ID
Chris A12
Amir B13
Raj C56
Soha Z11
Rabia D7
Bushra F9
Usman S7
Robert M8
Caroline N57
Bonnie X11

Other Queries:
20) ORDER BY:

By default, The ORDER BY keyword sorts the records in ascending order. The ORDER
BY keyword is used to sort the result in ascending or descending order. To sort the records in
descending order, We’ll use the DESC keyword.

 Lets see the actual table named as reviewer_table

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

SYNTAX:

SELECT * FROM Reviewer_Table


ORDER BY Rev_ID DESC;
 After applying query result is

Rev_ID Rev_Name Rev_Age


30 Bonnie 16
20 Caroline 18
19 Robert 32
18 Usman 35
17 Bushra 25
16 Rabia 22
15 Soha 26
14 Raj 25
13 Amir 30
12 Chris 20

21) Distinict :
We use this Distinct key word to remove the duplication in table.
 Lets see the original table named as Movie_Table

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Syntax:

SELECT DISTINCT Country FROM Movie_Table;


 After applying query result is:

Country
USA
USA
Pakistan
UK
UK
India
UK
South Korea
USA
Japan
Germany
Germany

22) Select Top:


Select TOP is use to get specific number of data. It is really helpful when you have a large
number of data and you want to get some of it.
 Lets see the example we have table as movie_table

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Syntax:
SELECT TOP 6 * FROM movie_table;

 After apply above mentioned query we will get following table.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India

23) Concatenation :
We use Concatenation to combine 2 strings from different columns and make sing string having
1 column hading.
 Lets have an example Actor_Table.

Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link


AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom

SYNTAX:
select Actor_FName + ‘ ,’ + Actor_LName as Actor_Name from Actor_Table
 After applying concatenation see the final column.

Actor_Name
Tom , Cruise
Raj , Kumar
Jahnvi , kapoor
Hanery , Cavil
Millie , Brown
Emma , Watson
Scarlet , Johanson
Robert , Pattinson
Elizbeth , Olsen
Chris , Evans
Loius , Hoffman
Park , Shinhye

24) MIN() :
We use Min() function to find the smallest value from selected column
 Lets see a example .we have table names as rating_table

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9
SYNTEX:
SELECT MIN(Rev_Stars) AS Lowest_Rev
FROM RATING_TABLE;

 After applying above function .


Lowest_Rev
2.5

25) MAX():
We use Max() function to find the largest value from selected column.
 Lets see a example .we have table names as rating_table

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

SYNTAX:
SELECT MAX(Rev_Stars) AS Highest_Rev
FROM RATING_TABLE;

 After applying above function .


Highest_Rev
4.8

26) LAST():
We will get Last Value from movie title of mavie_table.
 Lets see the example. We have movie_table.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

SYNTEX:
SELECT LAST(Movie_Title) AS Last_Movie
FROM Movie_Table;
 After applying function lets see the result.

Last_Movie
The Rain

27) FIRST():
We will get First Value from movie title of movie_table.
 Let’s see the example. We have movie_table.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

SYNTEX:
SELECT First(Movie_Title) AS First_Movie
FROM Movie_Table;
 After applying function lets see the result.

First_Movie
The Croods

28). SUM:
It is function used to return sum of values from a particular column

 Let see the example.


Reviewer Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Syntax:
SELECT SUM(Rev_Age)
FROM Reviewer_Table;
 The result of above Query is:

(NO Column Name)


1 249

29). AVG:
It is used to aggregate a numeric column and return its average.

 Let see the example.


Reviewer Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16

Syntax:

SELECT AVG(column_name)

FROM table_name;

 The result of above Query is:

(NO Column Name)


1 24.9
30). LIMIT:
It is a clause to specify the maximum number of rows the result set must have.
Let choose the Actor_Table and implement query on it.

Actor Table
Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link
AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB8 Daniel Radcliff Male Rad@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom

SELECT * FROM Actor_Table


LIMIT 4;
The resulting table will look like:

Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link


AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com

31).UNION:
With UNION, two SELECT statements can be combined into one result set (allowing
only distinct values).
 Let’s see the example. We have movie_table & Rating_Table respectively.
Movie_ID Movie_Title Release_Date Movie_Language Country
A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT Movie_ID FROM Movie_Table
UNION
SELECT Movie_ID FROM Rating_Table
ORDER BY Movie_ID;
 The resulting table will look like:
Movie_ID
A12
B13
C56
Z11
D7
F9
S7
M8
N57
X11
O9
A51

32). UNION ALL:


Using the UNION ALL command, you can combine the result of multiple SELECT
statements.
 Let’s see the example. We have movie_table & Rating_Table respectively.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT Movie_ID FROM Movie_Table
UNION ALL
SELECT Movie_ID FROM Rating_Table
ORDER BY Movie_ID;
 The resulting table will look like:

Movie_ID
A12
A12
B13
B13
C56
C56
Z11
Z11
D7
D7
F9
F9
S7
S7
M8
M8
N57
N57
X11
X11
O9
A51

Syntax:

SELECT City FROM Customers


UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

33). HAVING:
It is used in SQL because the WHERE keyword cannot be used in aggregating functions
 Let’s see the example. We have Rating_table.

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT COUNT(Rev_ID), Rev_Stars
FROM Rating_Table
GROUP BY Rev_Stars;
HAVING COUNT(Rev_ID) > 15
 After implementing the query, we obtain the following result.

Rev_ID Rev_Stars
16 4.8
17 5
18 3.2
19 4.2
20 4
30 3.9

34. Group By:


In SQL, Group By command is used for aggregating data with the SELECT
statement. The GROUP BY statement groups rows that have the same values into summary rows.
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
 Let’s see the example. We have Rating_table.

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT COUNT(Rev_ID), Rev_Stars
FROM Rating_Table
GROUP BY Rev_Stars;
The result set will look like:

Rev_ID Rev_Stars
12 4
13 3.5
14 2.5
15 3.7
16 4.8
17 5
18 3.2
19 4.2
30 3.9

The above table does not contain the row with Rev_ID = 20

35).WHERE :
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified
condition. The Where Cluase can be used with multiple operators such as: ‘AND’, ‘OR’ , ‘NOT’
,’IS NULL’, ‘IS NOT NULL’,’LIKE’, ’=’, ‘>’, ’<’, ’>=’, ‘<=’, ’+’,’-‘ etc. and many more.

 Let’s see the example. We have movie_table

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany
Syntax:
SELECT Movie_ID
FROM Movie_Table
WHERE Movie_Title LIKE ‘A%’;

 The result of this query will look like this:

Movie_Title
Alive
Aquaman
A silent voice

It showed the result of those Movie_Title whose name begin with letter ‘A’.

36). AS:
It is an keyword in SQL that is used to rename a column or table using an alias name.

Alias for Columns:


 Let’s see the example. We have movie_table

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Syntax:
SELECT Movie_Title AS Mov_Name
FROM Customers;

 The result of this query will look like this:


Mov_Name
The Croods
Mission Impossible
The Avengers
Witcher
Harry Potter
Roohi
Enola Holmes
Alive
Aquaman
A silent voice
Dark
The Rain
Alias for Tables
 Let’s see the example. We have movie_table & Rating_Table respectively.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

Movie_ID Rev_ID Rev_Stars


A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9

Syntax:
SELECT m.Movie_ID, m.Movie_Title, r.Rev_Stars
FROM Movie_Table AS m, Rating_Table AS r
WHERE m.Movie_Title = "Witcher" AND m.Movie_ID = r.Movie_ID;

 The result of this query will look like this:

Movie_ID Movie_Title Rev_Stars


Z11 Witcher 3.7

37).ROUND:
It is a function that takes the column name and a integer as an argument, and rounds the values in
a column to the number of decimal places specified by an integer.

Syntax:
SELECT ROUND(3.9, 1) AS Round_Value;

Round_Value
1 4.0

38). Auto Increment:


It allows a unique number to be generated automatically when a new record is inserted into a
table.

Syntax:

AttributeName dataType NOT NULL AUTO_INCREMENT(1,1);

Example:

Create Table Reviewer_Table(


Rev_ID int not null AUTO_INCREMENT(12,1);
Rev_Name Varchar(30),
Rev_Age int,
);
Here, after inserting data into all columns except Rev_ID. Following table will result. As we can
see Rev_ID is auto incremented, it starts from 12 and incremented by 1 in each row.

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
21 Bonnie 16

39). Like Operator:


The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.There are
two wildcards often used in conjunction with the LIKE operator:

The percent sign (%) represents zero, one, or multiple characters.

The underscore sign (_) represents one, single character.

LIKE Operator Description

WHERE Rev_Name LIKE 'a%' Finds any values that start with "a"

WHERE Rev_Name LIKE '%a' Finds any values that end with "a"
WHERE Rev_Name LIKE Finds any values that have "or" in any position
'%or%'

WHERE Rev_Name LIKE '_r%' Finds any values that have "r" in the second position

WHERE Rev_Name LIKE Finds any values that start with "a" and are at least 2 characters in
'a_%' length

WHERE Rev_Name LIKE Finds any values that start with "a" and are at least 3 characters in
'a__%' length

WHERE Rev_Name LIKE Finds any values that start with "a" and ends with "o"
'a%o'

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern

Example:
Select Rev_ID
FROM Reviewer_Table
WHERE Rev_Name Like %a;

Reviewer_Table
Rev_ID Rev_Name
15 Soha
16 Rabia
17 Bushra
40). In Operator:
The IN operator allows you to specify multiple values in a WHERE clause. We can also use
NOT IN statement.

Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

OR

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

Example:
SELECT * FROM Reviewer_Table
WHERE Rev_Age IN (35, 25, 16);

Reviewer_Table
Rev_ID Rev_Name Rev_Age
14 Raj 25
17 Bushra 25
18 Usman 35
21 Bonnie 16

41). Between Operator:


The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:
SELECT * FROM Rating_Table
WHERE Rev_Stars BETWEEN 4 AND 5;

Rating_Table
Movie_ID Rev_ID Rev_Stars
A12 12 4
D7 16 4.8
F9 17 5
M8 19 4.2
N57 20 4

42). LOCK TABLE:


The LOCK TABLE command is used to lock the table named sql_name in
either EXCLUSIVE or SHARE mode.

In EXCLUSIVE mode, the data of the table cannot be read or modified by another transaction.

In SHARE mode, the data of the table can be read by concurrent transactions but modifications
are still prohibited.

Example

 Let’s see the example. We have movie_table.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

This example locks the Movie_Table so that it can be read but not modified by other
transactions:

Syntax:
LOCK TABLE Movies_Table IN SHARE MODE;
43). UNLOCK TABLE:
The UNLOCK TABLE command is used to unlock a table that has previously been locked via
the LOCK TABLE command. It will not work if it is passed within a transaction or if it is used
on a table that is locked by another process.

Example

 Let’s see the example. We have movie_table & Rating_Table respectively.

Movie_ID Movie_Title Release_Date Movie_Language Country


A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany

This command removes the lock on the Movie_table:

Syntax:

UNLOCK TABLE Movie_Table;

44). IS NULL/ IS Not NULL:


We can test for NULL values with comparison operators, such as =, <, or <>.We will have to
use the IS NULL and IS NOT NULL operators instead.

 IS NULL Syntax:

The IS NULL operator is used to test for empty values (NULL values).
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

 IS NOT NULL Syntax:


The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

Example:
SELECT Rev_ID, Rev_Name, Rev_Age
FROM Reviewer_Table
WHERE Rev_Name IS NOT NULL;

Rev_ID Rev_Name Rev_Age


12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
21 Bonnie 16

45). Comparison Operators:


A comparison (or relational) operator is a mathematical symbol which is used to compare two
values. Comparison operators are used in conditions that compares one expression with another.

The following table describes different types of comparison operators -

Operator Description Operates on

= Equal to. Any compatible data types


> Greater than. Any compatible data types

< Less than. Any compatible data types

>= Greater than equal to. Any compatible data types

<= Less than equal to. Any compatible data types

<> Not equal to. Any compatible data types

Example:
“=”
SELECT Rev_Name, Rev_Age
FROM Reviewer_Table
WHERE Rev_ID = 20;

Rev_ID Rev_Name Rev_Age


20 Caroline 18

“<”
Select Rev_Name,Rev_ID
FROM Reviewer_Table
Where Rev_ID<15;

Rev_ID Rev_Name
12 Chris
13 Amir
14 Raj

“>”
Select Rev_Name,Rev_ID
FROM Reviewer_Table
Where Rev_ID>19;

Rev_ID Rev_Name
20 Caroline
21 Bonnie

“<=”
Select Rev_Name,Rev_ID
FROM Reviewer_Table
Where Rev_Age<=20;

Rev_ID Rev_Name Rev_Age


12 Chris 20
21 Bonnie 16

“>=”
Select Rev_Name,Rev_Age
FROM Reviewer_Table
Where Rev_ID>=15;

Rev_ID Rev_Name Rev_Age


15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
21 Bonnie 16

“<>”
Select *
FROM Reviewer_Table
Where Rev_age<25;
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
15 Soha 26
16 Rabia 22
18 Usman 35
19 Robert 32
20 Caroline 18
21 Bonnie 16

You might also like