0% found this document useful (0 votes)
13 views33 pages

Lecture #8 To 9

Uploaded by

iqbaloriakhil890
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)
13 views33 pages

Lecture #8 To 9

Uploaded by

iqbaloriakhil890
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/ 33

‫بسم هللا‬

‫الرحمن‬
‫الرحیم‬

SALAM University, Kabul


INTRODUCTION TO SQL
ADVANCE SQL

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

3
OUTCOMES
• DDL
• DQL
• DCL
• DML etc

SALAM University, Kabul


Adopted from Reference Book No 1

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

5 STRUCTURED QUERY LANGUAGE(SQL)

• SQL stands for Structured Query Language.

• SQL is a database language designed for the retrieval and management of


data in a relational database. SQL commands can be classified into
three/four types.

1. Data Definition Language (DDL).

2. Data Query Language (DQL) .

3. Data Manipulation Language (DML).

4. Data Control Language (DCL).

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

6
1. DATA DEFINITION LANGUAGE (DDL)
• DDL(Data Definition Language) : DDL or 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 and these commands are used to create, alter, and drop
tables etc. and they are covered first in this chapter.

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

7
TYPES OF CREATE STATEMENT
• There are two CREATE statements available in SQL:

• 1. CREATE DATABASE

• 2. CREATE TABLE

• 1. The CREATE DATABASE statement is used to create a new database in SQL.

• Syntax:

• CREATE DATABASE database_name;

• database_name: name of the database.

• Example Query:
This query will create a new database in SQL and name the database
as my_database.

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

8
CREATE STATEMENT
• 2. The CREATE TABLE statement is used to create a table. We know that
a table comprises of rows and columns. Syntax:

• CREATE TABLE table_name(


column1 data_type(size),
column2 data_type(size),……
);

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

9
EXAMPLE QUERY:
• This query will create a table named Students with three columns, ROLL_NO,
NAME and SUBJECT.

• CREATE TABLE Students

• (

• ROLL_NO int(3),

• NAME varchar(20),

• SUBJECT varchar(20),

• );

• This query will create a table named Students. The ROLL_NO field is of type int and
can store an integer number of size 3. The next two columns NAME and SUBJECT
are of type varchar and can store characters and the size 20 specifies that these two
fields can hold maximum of 20 characters.

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

10
DROP QUERY
• 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)

• Syntax:

• DROP object object_name

• Examples:

• DROP TABLE table_name;

• table_name: Name of the table to be deleted.

• DROP DATABASE database_name;

• database_name: Name of the database to be deleted.

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

11
2. DATA QUERY LANGUAGE (DQL) :
• The SELECT Statement in SQL is used to retrieve or fetch data from a
database. We can fetch either the entire table or according to some
specified rules. The data returned is stored in a result table.

• Example of DQL:

• SELECT – is used to retrieve data from the a database

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

12
BASIC SYNTAX OF (DQL) :
• SELECT column1,column2 FROM table_name

• column1 , column2: names of the fields of the table

• table_name: from where we want to fetch

• This query will return all the rows in the table with fields column1 ,
column2.

• Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:

• SELECT ROLL_NO, NAME, AGE FROM Student;

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

13
EXAMPLE OF (DQL) :

Output:

ROLL_NO NAME Age

1 Ram 18

2 RAMESH 18

3 SUJIT 20

4 SURESH 18

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

14
EXAMPLE OF (DQL) :
• To fetch all the fields from the table Student:
• SELECT * FROM Student;
• Output:

ROLL_NO NAME ADDRESS PHONE Age

1 Ram Delhi XXXXXXXXXX 18

2 RAMESH GURGAON XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

4 SURESH Delhi XXXXXXXXXX 18

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

15 SELECT STATEMENT

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

16
USING THE WHERE CLAUSE:

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

17
USING THE WHERE CLAUSE:

• Character strings and date values are enclosed with single


quotation marks.
• Character values are case-sensitive and date values are format-
sensitive.

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

18 4. DATA MANIPULATION LANGUAGE (DML)

• 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.

• Examples of DML:
• INSERT – is used to insert data into a table.
• UPDATE – is used to update existing data within a table.

• DELETE – is used to delete records from a database table.

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

19
BEFORE INSERT STATEMENT OF SQL

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

20
INSERT STATEMENT OF SQL
• The INSERT INTO statement of SQL is used to insert a new row
in a table. There are two ways of using INSERT INTO statement
for inserting rows:

• Method 1 (Inserting only values) :


• 1. Only values: First method is to specify only the value of data to
be inserted without the column names.

• INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST


BENGAL’,’XXXXXXXXXX’,’19’)

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

21
OUTPUT: THE TABLE STUDENT WILL NOW LOOK LIKE:

ROLL_NO NAME ADDRESS PHONE Age

1 Ram Delhi XXXXXXXXXX 18

2 RAMESH GURGAON XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

4 SURESH Delhi XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

2 RAMESH GURGAON XXXXXXXXXX 18

5 HARSH WEST BENGAL XXXXXXXXXX 19

SALAM University, Kabul


Abasyn University, Peshawar

22

• Method 2 (Inserting values in only specified columns):


• INSERT INTO Student (ROLL_NO, NAME, Age) VALUES
(‘5′,’PRATIK’,’19’);
• Output in next slide.

SALAM University, Kabul


Abasyn University, Peshawar

23
OUTPUT:THE TABLE STUDENT WILL NOW LOOK LIKE:

ROLL_NO NAME ADDRESS PHONE Age

1 Ram Delhi XXXXXXXXXX 18

2 RAMESH GURGAON XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

4 SURESH Delhi XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

2 RAMESH GURGAON XXXXXXXXXX 18

5 PRATIK null null 19

SALAM University, Kabul


Abasyn University, Peshawar

24
DELETE STATEMENT
• The DELETE Statement in SQL is used to delete existing records from
a table. We can delete a single record or multiple records depending on
the condition we specify in the WHERE clause.
• Basic Syntax:

• DELETE FROM table_name WHERE some_condition;

• table_name: name of the table

• some_condition: condition to choose particular record

SALAM University, Kabul


Abasyn University, Peshawar

25
BEFORE DELETE STATEMENT TABLE

SALAM University, Kabul


Abasyn University, Peshawar

26
DELETE STATEMENT
• Deleting single record: Delete the rows where NAME = ‘Ram’. This will delete
only the first row.

• DELETE FROM Student WHERE NAME = 'Ram';

• Output:

• The above query will delete only the first row and the table Student will now
look like,

SALAM University, Kabul


Abasyn University, Peshawar

27
AFTER DELETE STATEMENT TABLE
ROLL_NO NAME ADDRESS PHONE Age

2 RAMESH GURGAON XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

4 SURESH Delhi XXXXXXXXXX 18

3 SUJIT ROHTAK XXXXXXXXXX 20

2 RAMESH GURGAON XXXXXXXXXX 18

SALAM University, Kabul


Abasyn University, Peshawar

28
UPDATE STATEMENT
• The UPDATE statement in SQL is used to update the data of an existing table
in database. We can update single columns as well as multiple columns using
UPDATE statement as per our requirement: Basic Syntax

• UPDATE table_name SET column1 = value1, column2 = value2,...

• WHERE condition;

• table_name: name of the table

• column1: name of first , second, third column....

• value1: new value for first, second, third column....

• condition: condition to select the rows for which the

• values of columns needs to be updated.

SALAM University, Kabul


Abasyn University, Peshawar

29
BEFORE UPDATE STATEMENT

SALAM University, Kabul


Abasyn University, Peshawar

30
UPDATE STATEMENT
• Updating single column: Update the column NAME and set the value
to ‘PRATIK’ in all the rows where Age is 20.

• UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;


• Output:
This query will update two rows(third row and fifth row) and the
table Student will now look like,

SALAM University, Kabul


Abasyn University, Peshawar

31
BEFORE UPDATE STATEMENT
ROLL_NO NAME ADDRESS PHONE Age

1 Ram Delhi XXXXXXXXXX 18

2 RAMESH GURGAON XXXXXXXXXX 18

3 PRATIK ROHTAK XXXXXXXXXX 20

4 SURESH Delhi XXXXXXXXXX 18

3 PRATIK ROHTAK XXXXXXXXXX 20

2 RAMESH GURGAON XXXXXXXXXX 18

SALAM University, Kabul


32
SUMMARY
• A database management system is a software package for creating and managing
databases.

• SQL is a database computer language designed for the retrieval and management of
data in a relational database.

• DDL or Data Definition Language actually consists of the SQL commands that can
be used to define the database schema.

• The purpose of DQL Command is to get some schema relation based on the query
passed to it.

• The SQL commands that deals with the manipulation of data present
in the database belong to DML

SALAM University, Kabul


SALAM University, Kabul December 13, 2024

33

THE END

SALAM University, Kabul

You might also like