0% found this document useful (0 votes)
39 views18 pages

SQL Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views18 pages

SQL Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

05/06/2024

Introduction
to SQL
• SQL stands for Structured Query
Language

• SQL is a standard language for


storing, manipulating and
retrieving data in databases.

• SQL became a standard of the


American National Standards

SQL Institute (ANSI) in 1986, and of


the International Organization
STRUCTURE QUERY LANGUAGE for Standardization (ISO) in
1987.
1 2
PREPARED BY: ARMR.AZAM

What Can SQL do? DDL – Data Definition


Language
• SQL can execute queries against a database

• SQL can retrieve data from a database

• SQL can insert records in a database DQl – Data Query Language


These SQL commands
• SQL can update records in a database
are mainly categorized
• SQL can delete records from a database into four categories as:
• SQL can create new databases DML – Data Manipulation
Language
• SQL can create new tables in a database

• SQL can create stored procedures in a database

• SQL can create views in a database


DCL – Data Control Language
• SQL can set permissions on tables, procedures,
and views

3 4
05/06/2024

DDL (Data Definition Language)


DQL (Data Query Language)
• Data Definition Language actually consists of the SQL commands that can be used to
define the database schema. It simply deals with descriptions of the database schema • DQL statements are used for performing queries on the data within schema objects. The

and is used to create and modify the structure of database objects in the database. purpose of the DQL Command is to get some schema relation based on the query passed to

DDL is a set of SQL commands used to create, modify, and delete database structures it. We can define DQL as follows it is a component of SQL statement that allows getting data
but not data. These commands are normally not used by a general user, who should be from the database and imposing order upon it. It includes the SELECT statement. This
accessing the database via an application. command allows getting the data out of the database to perform operations with it. When a
• List of DDL commands: SELECT is fired against a table or tables the result is compiled into a further temporary table,
• CREATE: This command is used to create the database or its objects (like which is displayed or perhaps received by the program i.e. a front-end.
table, index, function, views, store procedure, and triggers).

• DROP: This command is used to delete objects from the database. • List of DQL:
• ALTER: This is used to alter the structure of the database.
• SELECT: It is used to retrieve data from the database.

5 6

DML(Data Manipulation Language)


• The SQL commands that deals with the manipulation of data present
in the database belong to DML or Data Manipulation Language and
this includes most of the SQL statements. It is the component of the
SQL statement that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements.
• List of DML commands:
• INSERT : It is used to insert data into a table.
• UPDATE: It is used to update existing data within a table.
• DELETE : It is used to delete records from a database table.

7 8
05/06/2024

SQL
CREATE The SQL CREATE
DATABASE Statement

ALTER The CREATE TABLE statement


is used to create a new table
in a database.
DROP
Syntax:

CREATE DATABASE databasename;


9 10

The CREATE TABLE statement is used to create a new

1. Create a database call “sample”. table in a database.

Syntax

CREATE TABLE table_name (


column1 datatype,

ANSWER:
The SQL column2 datatype,
CREATE TABLE column3 datatype,
CREATE DATABASE SAMPLE;
Statement ....
);

The column parameters specify the names of the


columns of the table.

The datatype parameter specifies the type of data the


column can hold (e.g. varchar, integer, date, etc.).
11 12
05/06/2024

Data Types - String Data Types Numeric Data Types


Data type Description Data type Description
BIT(size) A bit-value type. The number of bits per value is specified in size. The size parameter can hold a value from 1 to 64. The
CHAR(size) A FIXED length string (can contain letters, numbers, and special characters). The size parameter specifies the default value for size is 1.
column length in characters - can be from 0 to 255. Default is 1 TINYINT(size) A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255. The size parameter specifies the
maximum display width (which is 255)
VARCHAR(size) A VARIABLE length string (can contain letters, numbers, and special characters). The size parameter specifies the
BOOL Zero is considered as false, nonzero values are considered as true.
maximum string length in characters - can be from 0 to 65535
BOOLEAN Equal to BOOL
BINARY(size) Equal to CHAR(), but stores binary byte strings. The size parameter specifies the column length in bytes. Default is SMALLINT(size) A small integer. Signed range is from -32768 to 32767. Unsigned range is from 0 to 65535. The size parameter specifies the
1 maximum display width (which is 255)
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from 0 to 16777215. The size parameter
VARBINARY(size) Equal to VARCHAR(), but stores binary byte strings. The size parameter specifies the maximum column length in
specifies the maximum display width (which is 255)
bytes.
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to 4294967295.
TINYBLOB For BLOBs (Binary Large Objects). Max length: 255 bytes The size parameter specifies the maximum display width (which is 255)

TINYTEXT Holds a string with a maximum length of 255 characters INTEGER(size) Equal to INT(size)
TEXT(size) Holds a string with a maximum length of 65,535 bytes BIGINT(size) A large integer. Signed range is from -9223372036854775808 to 9223372036854775807. Unsigned range is from 0 to
18446744073709551615. The size parameter specifies the maximum display width (which is 255)
BLOB(size) For BLOBs (Binary Large Objects). Holds up to 65,535 bytes of data

MEDIUMTEXT Holds a string with a maximum length of 16,777,215 characters FLOAT(size, d) A floating point number. The total number of digits is specified in size. The number of digits after the decimal point is
specified in the d parameter. This syntax is deprecated in MySQL 8.0.17, and it will be removed in future MySQL versions
MEDIUMBLOB For BLOBs (Binary Large Objects). Holds up to 16,777,215 bytes of data
FLOAT(p) A floating point number. MySQL uses the p value to determine whether to use FLOAT or DOUBLE for the resulting data
LONGTEXT Holds a string with a maximum length of 4,294,967,295 characters
type. If p is from 0 to 24, the data type becomes FLOAT(). If p is from 25 to 53, the data type becomes DOUBLE()
LONGBLOB For BLOBs (Binary Large Objects). Holds up to 4,294,967,295 bytes of data
DOUBLE(size, d) A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal
ENUM(val1, val2, val3, ...) A string object that can have only one value, chosen from a list of possible values. You can list up to 65535 point is specified in the d parameter

values in an ENUM list. If a value is inserted that is not in the list, a blank value will be inserted. The values are DOUBLE PRECISION(size, d)

sorted in the order you enter them DECIMAL(size, d) An exact fixed-point number. The total number of digits is specified in size. The number of digits after the decimal point is
specified in the d parameter. The maximum number for size is 65. The maximum number for d is 30. The default value
SET(val1, val2, val3, ...) A string object that can have 0 or more values, chosen from a list of possible values. You can list up to 64 values for size is 10. The default value for d is 0.
13 14
in a SET list DEC(size, d) Equal to DECIMAL(size,d)

Date and Time Data Types


Data type Description
2. Create a table call “STUDENT” with following fields and
DATE A date. Format: YYYY-MM-DD. The supported range is from '1000-01-01' to
'9999-12-31' suitable data types “SID, SNAME, AGE, PAYMENT”.
DATETIME(fsp) A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The
supported range is from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
Adding DEFAULT and ON UPDATE in the column definition to get ANSWER:
automatic initialization and updating to the current date and time

TIMESTAMP(fsp) A timestamp. TIMESTAMP values are stored as the number of seconds


since the Unix epoch ('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD
CREATE TABLE STUDENT(SID VARCHAR(3) PRIMARY KEY,
hh:mm:ss. The supported range is from '1970-01-01 00:00:01' UTC to '2038-
01-09 03:14:07' UTC. Automatic initialization and updating to the current SNAME VARCHAR (20),
date and time can be specified using DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP in the column definition
AGE INT,
PAYMENT DOUBLE);
TIME(fsp) A time. Format: hh:mm:ss. The supported range is from '-838:59:59' to
'838:59:59'
YEAR A year in four-digit format. Values allowed in four-digit format: 1901 to
2155, and 0000.
MySQL 8.0 does not support year in two-digit format.
15 16
05/06/2024

The ALTER TABLE statement is used to add, delete, or modify


columns in an existing table.
3. Delete the column call “payment” in student table.
• The ALTER TABLE statement is also used to add and drop
various constraints on an existing table.
ANSWER:
The SQL ALTER TABLE - DROP COLUMN ALTER TABLE STUDENT

ALTER TABLE • To delete a column in a table, use the following syntax


(notice that some database systems don't allow deleting a
DROP COLUMN PAYMENT;
Statement column):
ALTER TABLE table_name
DROP COLUMN column_name;

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
17 18

4. Add the column call “payment” in student table with


suitable data type. The DROP TABLE statement is used
to drop an existing table in a
ANSWER:
database.
ALTER TABLE STUDENT
The SQL Syntax
ADD COLUMN PAYMENT DOUBLE;
DROP TABLE DROP TABLE table_name;
Statement Note: Be careful before dropping
a table. Deleting a table will result
in loss of complete information
stored in the table!

19 20
05/06/2024

5. Delete the table call student.

INSERT
ANSWER:

SQL
DROP TABLE STUDENT;
UPDATE

DELETE

21 22

The INSERT INTO statement is used to insert new records in a


1. Insert a record “OW7", "Nimal", "0775688133” into
table.
owner table.
INSERT INTO Syntax

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


1. Specify both the column names and the values to be
INSERT INTO OWNER(OID, ONAME, CONTACT)
The SQL inserted:

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


INSERT INTO VALUES (value1, value2, value3, ...);
VALUES ("OW7", "NIMAL", "0775688133");

Statement 2. If you are adding values for all the columns of the table, OUTPUT: OID ONAME CONTACT
you do not need to specify the column names in the SQL OW1 Kamal 0775658568
query. However, make sure the order of the values is in the OW2 Mala 0775658511
same order as the columns in the table. Here, the INSERT OW3 Raja 0776658786
INTO syntax would be as follows: OW4 Nimesh 0775325548

INSERT INTO table_name OW5 Raja 0771112336

VALUES (value1, value2, value3, ...); OW6 Raahul 0715588568


23 OW7 Nimal 0775688133 24
05/06/2024

2. Insert a record “OW8", "Rajesh", "0775686599” into


The UPDATE statement is used to modify the existing
owner table.
records in a table.

ANSWER: UPDATE Syntax

INSERT INTO INSERT INTO owner UPDATE table_name

VALUES VALUES ("OW8", "Rajesh", "0775686599"); SQL UPDATE SET column1 = value1, column2 = value2, ...
WHERE condition;
Statement
OUTPUT: OID ONAME CONTACT Note: Be careful when updating records in a table!
OW1 Kamal 0775658568
Notice the WHERE clause in the UPDATE statement.
OW2 Mala 0775658511
OW3 Raja 0776658786 The WHERE clause specifies which record(s) that
OW4 Nimesh 0775325548
should be updated. If you omit the WHERE clause, all
OW5 Raja 0771112336
OW6 Raahul 0715588568 records in the table will be updated!
OW7 Nimal 0775688133
25 26
OW8 Rajesh 0775686599

The DELETE statement is used to delete existing


3. Change the name of “OW8” as “Kamelesh” records in a table.

ANSWER:
DELETE Syntax
UPDATE OWNER
SET ONAME = "KAMELESH" DELETE FROM table_name

WHERE OID = "OW8";


SQL DELETE WHERE condition;

OUTPUT: OID ONAME CONTACT


Statement
OW1 Kamal 0775658568
Note: Be careful when deleting records in a table!
OW2 Mala 0775658511
OW3 Raja 0776658786 Notice the WHERE clause in the DELETE statement.
OW4 Nimesh 0775325548
The WHERE clause specifies which record(s) should
OW5 Raja 0771112336
OW6 Raahul 0715588568 be deleted. If you omit the WHERE clause, all
OW7 Nimal 0775688133
27
records in the table will be deleted! 28
OW8 Kamelesh 0775686599
05/06/2024

25. Delete the record of “OW8”.

ANSWER:

DELETE FROM owner


The SQL SELECT Statement
WHERE OID = "OW8";
• The SELECT statement is used to select data from a
OUTPUT: OID ONAME CONTACT
database.
OW1 Kamal 0775658568
OW2 Mala 0775658511 • The data returned is stored in a result table, called
OW3 Raja 0776658786 the result-set.
OW4 Nimesh 0775325548
OW5 Raja 0771112336 SELECT Syntax.
OW6 Raahul 0715588568 SELECT column1, column2, ...
OW7 Nimal 0775688133
29 FROM table_name; 30

1. Retrieve EMPID, EMPNAME from EMP Table.

EMPID EMPNAME CONTACTNO DIPID


OUTPUT:
E001 Kamal 0775658568 D01
EMPID EMPNAME
Table Name: E002 Mala 0775658511 D02
ANSWER: E001 Kamal
EMP E003
E004
Nimesh
Rahu
0776658786
0775325548
D01
D03
E002 Mala
SELECT EMPID, EMPNAME E003 Nimesh
E005 Kamal 0771112336 D02
E004 Rahu
E006 Leela 0715588568 D01 FROM EMP; E005 Kamal
E006 Leela

31 32
05/06/2024

2. Retrieve all the details from EMP Table. 3. Retrieve EMPID, EMPNAME, DIPID from EMP Table.

OUTPUT: OUTPUT:
EMPID EMPNAME CONTACTNO DIPID EMPID EMPNAME DIPID

ANSWER: E001 Kamal 0775658568 D01 ANSWER: E001 Kamal D01


E002 Mala 0775658511 D02 E002 Mala D02
SELECT * E003 Nimesh 0776658786 D01 SELECT EMPID, EMPNAME, DIPID E003 Nimesh D01
E004 Rahu 0775325548 D03 E004 Rahu D03
FROM EMP; E005 Kamal 0771112336 D02 FROM EMP; E005 Kamal D02
E006 Leela 0715588568 D01 E006 Leela D01

33 34

4. Retrieve all the details from DEPARTMENT Table.

DID DNAME OUTPUT:

Table Name: D01 Accounts ANSWER:


DID DNAME

DEPARTMENT D02 Administration


D01 Accounts
SELECT *
D02 Administration
D03 Transport
FROM DEPARTMENT;
D03 Transport

35 36
05/06/2024

SQL ARITHMETIC OPERATORS SQL COMPARISON OPERATORS


S
Operator Description Operator Description Q
+ Add = Equal to
L
CLAUSES - Subtract > Greater than

WORK *
/
Multiply
Divide
<
>=
Less than
Greater than or equal to O
TOGETHER % Modulo <= Less than or equal to
P
WITH SQL
<> Not equal to
SQL LOGICAL OPERATORS E
SELECT
Operator Description
SQL BITWISE OPERATORS R
A
AND TRUE if all the conditions separated by AND is TRUE

CLAUSE BETWEEN TRUE if the operand is within the range of


comparisons
Operator Description
IN TRUE if the operand is equal to one of a list of
expressions
& Bitwise AND T
LIKE
NOT
TRUE if the operand matches a pattern
Displays a record if the condition(s) is NOT TRUE
| Bitwise OR O
37
OR TRUE if any of the conditions separated by OR is
TRUE
^ Bitwise exclusive OR 38 R

5. Retrieve empid, empname of kamal from emp


The WHERE clause is used Table.
to filter records.

The SQL
WHERE Syntax:
ANSWER:
WHERE
SELECT column1, column2, ... OUTPUT:
FROM table_name EMPID EMPNAME
SELECT EMPID, EMPNAME
Clause WHERE condition;
FROM EMP
E001 Kamal
E005 Kamal
WHERE EMPNAME = “Kamal”;
Note: The WHERE clause is not only
used in SELECT statements, it is also
used in UPDATE, DELETE, etc.!
39 40
05/06/2024

7. Retrieve did and dname of D01 from department


6. Retrieve all the details of kamal from emp Table.
Table.

OUTPUT: ANSWER:
ANSWER:
OUTPUT:
EMPID EMPNAME CONTACTNO DIPID
SELECT * SELECT DID, DNAME DID DNAME
E001 Kamal 0775658568 D01
D01 Accounts
FROM EMP E005 Kamal 0771112336 D02 FROM DEPARTMENT
WHERE EMPNAME = “Kamal”; WHERE DID = “D01”;

41 42

8. Retrieve all the details of D03 from department


Table.

EMPID ALLOWANCE BASICSAL


E001 500.00 30,000.00
ANSWER:
OUTPUT:
Table Name: E002 1,000.00 35,000.00

SELECT * DID DNAME SALARY E003


E004
500.00
1,000.00
25,000.00
40,000.00
D03 Transport
FROM DEPARTMENT E005 1,000.00 42,000.00
E006 2,000.00 55,000.00
WHERE DID = “D03”;

43 44
05/06/2024

9. Retrieve all the details of employee who get more 10. Retrieve all the details of employee who get more
than 1,000.00 allowance from salary Table. than 500.00 allowance from salary Table.

OUTPUT:
ANSWER: ANSWER:
OUTPUT: EMPID ALLOWANCE BASICSAL
SELECT * EMPID ALLOWANCE BASICSAL SELECT * E002 1,000.00 35,000.00
E006 2,000.00 55,000.00 E004 1,000.00 40,000.00
FROM SALARY FROM SALARY
E005 1,000.00 42,000.00
WHERE ALLOWANCE > 1000; WHERE ALLOWANCE > 500; E006 2,000.00 55,000.00

45 46

• The WHERE clause can be combined with AND, OR, 11. Retrieve all the details of employee who get more than
and NOT operators.
500.00 allowance and more than 40,000.00 basicsal from
salary Table.
• The AND and OR operators are used to filter records
based on more than one condition:
ANSWER:
The SQL • The AND operator displays a record if all the conditions
separated by AND are TRUE. SELECT *
AND, OR
and NOT • The OR operator displays a record if any of the
conditions separated by OR is TRUE.
FROM SALARY

Operators • The NOT operator displays a record if the condition(s) is


WHERE ALLOWANCE > 500 AND BASICSAL > 40000;
NOT TRUE.
OUTPUT:
AND Syntax
EMPID ALLOWANCE BASICSAL
SELECT column1, column2, ...
FROM table_name
E005 1,000.00 42,000.00
WHERE condition1 AND condition2 AND condition3 ...; E006 2,000.00 55,000.00
47 48
05/06/2024

12. Retrieve all the details of Kamal who works in D01


from department Table. • The ORDER BY keyword is used to sort the result-set
in ascending or descending order.

ANSWER:
• The ORDER BY keyword sorts the records in
SELECT * The SQL ascending order by default. To sort the records in

FROM EMP ORDER BY descending order, use the DESC keyword.

Keyword
WHERE EMPNAME = “Kamal” AND DIPID = “D01”; ORDER BY Syntax

SELECT column1, column2, ...


OUTPUT:
FROM table_name
EMPID EMPNAME CONTACTNO DIPID
ORDER BY column1, column2, ... ASC|DESC;
E001 Kamal 0775658568 D01
49 50

13. Retrieve all the details of employees in emp table, 14. Retrieve all the details of employees in emp table,
sorted by empname in ascending order sorted by empname in descending order

OUTPUT: OUTPUT:
EMPID EMPNAME CONTACTNO DIPID EMPID EMPNAME CONTACTNO DIPID
ANSWER: ANSWER:
E001 Kamal 0775658568 D01 E004 Rahu 0775325548 D03
SELECT * E005 Kamal 0771112336 D02 SELECT * E003 Nimesh 0776658786 D01
E006 Leela 0715588568 D01 E002 Mala 0775658511 D02
FROM EMP E002 Mala 0775658511 D02 FROM EMP E006 Leela 0715588568 D01
E003 Nimesh 0776658786 D01 E001 Kamal 0775658568 D01
ORDER BY EMPNAME ASC; ORDER BY EMPNAME DESC;
E004 Rahu 0775325548 D03 E005 Kamal 0771112336 D02

51 52
05/06/2024

• The GROUP BY statement groups rows that have the


15. Find number of employees worked in each
same values into summary rows, like "find the number of
department
customers in each country".

• The GROUP BY statement is often used with aggregate


ANSWER:
The SQL functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to
OUTPUT:
COUNTOFEMP DIPID
GROUP BY group the result-set by one or more columns. SELECT COUNT(EMPID) AS COUNTOFEMP, DIPID
3 D01
Statement GROUP BY Syntax
FROM EMP 2 D02
SELECT column_name(s) 1 D03
FROM table_name GROUP BY DIPID;
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
53 54

• The HAVING clause was added to SQL


16. Find number of employees worked in each department
and sorted by count value in low to high because the WHERE keyword cannot be
used with aggregate functions.

ANSWER:
SELECT COUNT(EMPID) AS COUNTOFEMP, DIPID
OUTPUT:
COUNTOFEMP DIPID
The SQL HAVING Syntax

FROM EMP 1 D03


HAVING SELECT column_name(s)
2 D02 Clause FROM table_name
GROUP BY DIPID 3 D01
WHERE condition
ORDER BY COUNT(EMPID) ASC;
GROUP BY column_name(s)
HAVING condition

55
ORDER BY column_name(s); 56
05/06/2024

17. Find the department that more one employees'


work

ANSWER:
OUTPUT:
SELECT COUNT(EMPID) AS COUNTOFEMP, DIPID
COUNTOFEMP DIPID
FROM EMP
2 D02
GROUP BY DIPID 3 D01

HAVING COUNT(EMPID) > 1

ORDER BY COUNT(EMPID) ASC;

57 58

SQL JOIN
A JOIN clause is used to combine rows from two or
SQL INNER JOIN Keyword
more tables, based on a related column between
them. The INNER JOIN keyword selects records that have
Different Types of SQL JOINs matching values in both tables.
• (INNER) JOIN: Returns records that have
matching values in both tables
INNER JOIN Syntax
• LEFT (OUTER) JOIN: Returns all records from
the left table, and the matched records from SELECT column_name(s)
the right table FROM table1
• RIGHT (OUTER) JOIN: Returns all records from INNER JOIN table2
the right table, and the matched records from
ON table1.column_name = table2.column_name;
the left table

• FULL (OUTER) JOIN: Returns all records when


there is a match in either left or right table 59 60
05/06/2024

OID ONAME CONTACT VID MODEL COLOR OWNER


OW1 Kamal 0775658568 V001 Van White OW1
Table Name: OW2 Mala 0775658511 Table Name: V002 Car Black OW2

OWNER OW3
OW4
Raja
Nimesh
0776658786
0775325548
VEHICLE V003
V004
Van
Car
Blue
White
OW3
OW4
OW5 Raja 0771112336 V005 Three wheel Yellow OW5
OW6 Raahul 0715588568 V006 Van White OW6

61 62

18. Retrieve vid, model, color, bid and bdate from


vehicle and booking tables.
ANSWER:
SELECT VEHICLE.VID, MODEL, COLOR, BID, BDATE
BID BDATE VID FROM VEHICLE
Table Name: B01 12/01/2020 V01 INNER JOIN BOOKING
BOOKING B02 12/01/2020 V02
ON VEHICLE.VID = BOOKING.VID;
B03 15/02/2020 V01
OUTPUT:
B05 20/05/2020 V03
VID MODEL COLOR BID BDATE
V001 Van White B01 12/01/2020
V001 Van White B03 15/02/2020
V002 Car Black B02 12/01/2020
V003 Van Blue B04 20/05/2020
63 64
05/06/2024

19. Retrieve oid, oname, contact, vid, model, color, bid SQL LEFT JOIN Keyword
and bdate from owner, vehicle and booking tables.
ANSWER: The LEFT JOIN keyword returns all records from the left table

SELECT OID, ONAME, CONTACT, VEHICLE.VID, MODEL, COLOR, BID, BDATE (table1), and the matching records from the right table

FROM ((VEHICLE (table2). The result is 0 records from the right side, if there is no

INNER JOIN BOOKING ON VEHICLE.VID = BOOKING.VID) match.

INNER JOIN OWNER ON VEHICLE.OWNER = OWNER.OID); LEFT JOIN Syntax


OUTPUT: SELECT column_name(s)
VID MODEL COLOR BID BDATE
FROM table1
V001 Van White B01 12/01/2020
LEFT JOIN table2
V001 Van White B03 15/02/2020
ON table1.column_name = table2.column_name;
V002 Car Black B02 12/01/2020
V003 Van Blue B04 20/05/2020 Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
65 66

20. Retrieve vid, model, color, bid and bdate from SQL RIGHT JOIN Keyword
vehicle and booking tables (use left join).
ANSWER: The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1).
SELECT VEHICLE.VID, MODEL, COLOR, BID, BDATE
The result is 0 records from the left side, if there is no match.
FROM VEHICLE LEFT JOIN BOOKING ON VEHICLE.VID = BOOKING.VID; RIGHT JOIN Syntax

OUTPUT: SELECT column_name(s)


VID MODEL COLOR BID BDATE
FROM table1
V001 Van White B01 12/01/2020
V001 Van White B03 15/02/2020
RIGHT JOIN table2
V002 Car Black B02 12/01/2020 ON table1.column_name = table2.column_name;
V003 Van Blue B04 20/05/2020
V004 Car White
V005 Three Wheel Yellow Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
V006 Van White 67 68
05/06/2024

21. Retrieve vid, model, color, bid and bdate from


vehicle and booking tables (use right join).

ANSWER: A self join is a regular join, but the

SELECT VEHICLE.VID, MODEL, COLOR, BID, BDATE table is joined with itself.

Self Join Syntax


FROM VEHICLE RIGHT JOIN BOOKING ON VEHICLE.VID = BOOKING.VID; SQL Self Join
SELECT column_name(s)

OUTPUT: FROM table1 T1, table1 T2


VID MODEL COLOR BID BDATE
WHERE condition;
V001 Van White B01 12/01/2020
V001 Van White B03 15/02/2020
V002 Car Black B02 12/01/2020
V003 Van Blue B04 20/05/2020
69 70

22. Retrieve vid, model, color, bid and bdate from


vehicle and booking tables (use right join).
ANSWER:
SELECT VEHICLE.VID, MODEL, COLOR, BID, BDATE
FROM VEHICLE, BOOKING
WHERE VEHICLE.VID = BOOKING.VID;

OUTPUT:
VID MODEL COLOR BID BDATE
V001 Van White B01 12/01/2020
V001 Van White B03 15/02/2020
V002 Car Black B02 12/01/2020
V003 Van Blue B04 20/05/2020
71

You might also like