0% found this document useful (0 votes)
5 views

SQL (Structured Query Langauge)

Uploaded by

abhishek990ajsvv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL (Structured Query Langauge)

Uploaded by

abhishek990ajsvv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Data Definition Language (DDL)

DDL contains commands that are used to


SQL (Structured create tables.

Query Langauge) Example – Create, Alter, Rename and Drop

Data Manipulation Language (DML)


Structured Query Language (SQL)
DML commands are used to manipulate
It is a query language used for creating, data in tables.
storing and managing data in relational
Example – Select, Insert, Delete and
database.
Update.
Advantages of SQL
Data Control Language (DCL)
✓ SQL is portable and it has very high
It contains statements to assign security
speed.
levels in database which includes multiple
✓ Large volume of database can be
user setup.
handled easily.
✓ SQL can be linked to most of the Example –Grant and Revoke
high level languages for back end
operation. Transaction Control Language (TCL)

SQL Commands/Statements It is used to control transaction in a


database system.
SQL statements or commands are special
kind of statements that contains some Example – Commit, Savepoint and Rollback.
instructions and all statements ends with
Rules for SQL commands/Statements
semicolon.
✓ SQL commands are not case
SQL commands are divided into four parts
sensitive.
✓ Commands can be typed in single or
multiple lines.
✓ Comma is used to separate
parameters.
✓ Semicolon (;) is used to terminate
SQL statements.
✓ Characters and Date constants
(literals) must be enclosed with
single or double quotes.
Data Types in SQL column1 datatype (size) constraints,

Data Syntax Example column2 datatype(size) constraints, ----- );


Types
Char, Char(30) Name char(30) Example
Varchar Varchar(500) Address
varchar(500) Create table student(
Integer Int Roll int
rollno int primary key,
Float Float(5,2) Marks float(5,2)
Date Date Date date name char(20) unique,

marks int not null);


Software – MySQL (Command Line Client)
Table Constraints
Database Commands
These are the conditions which ensures we
Displaying Databases list are entering correct data in our relational
database/table.
Show databases;
1. Primary Key
Creating Database 2. Not null
3. Unique
Syntax - Create database database_name;
4. Check
Example - Create database school; 5. Default
6. Foreign key
Deleting Database
Example
Syntax - drop database database_name;
Create a Table “Student” with following
Example - drop database school; data

Using Database Rollno Primary key


Name Not null
Syntax – use database_name; Marks Not null and check for less than
101
Aadharno Unique
Grade Default value is ‘E’
DDL Commands
Creating Tables Create table Student (Rollno int primary
key, name char (30) not null, marks int not
Create table table_name ( null check (marks<101), aadharno int
unique, grade char default ‘E’;
Foreign Key Alter table student change grade gradenew
char;
Create table new (adno int, name char(30),
rollno int references class12 (Rollno)); Changing datatype
Describing Table Details Alter table table_name modify column
datatype[size];
Describe class12;
Alter table student modify name char(30);
Alter command
Changing table name
(Use to make changes in already existing
Table) Alter table old_table_name rename to
new_table_name;

Alter table student rename to newstudent;


Adding New Column or Primary Key
Deleting a table
Alter table table_name add
new_column_name datatype[size] Drop table table_name;
constraint;
Drop table school;
Alter table table_name add primary
key(column_name)
Creating table from existing table
Example
Create table new_table_name select
Alter table student add rank char default
column1, column2, _ _ _ from
‘E’;
existing_table_name;
Alter table student add primary key(rollno);
Create table new select name, rollno, marks
Removing Column or Primary Key from student;

Alter table table_name drop primary DML Commands


key/column_name;
Sample Table (Student)
Alter table student drop primary key, drop
column_name; Student
Rollno Name Mark Percentag Dob
Changing column name (Primar s e
y Key)
Alter table table_name change old_name 101 Karan 98 92.6 2004
new_name datatype; -01-
23 Rolln Name Mark Percentag Dob
103 Diya 89 98.4 2005 o s e
-06- 101 Karan 98 92.6 2004
30 -01-
105 Riya 96 94.2 2004 23
-03- 103 Diya 89 98.4 2005
26 -06-
108 Deepa 92 96.6 2004 30
k -04- 105 Riya 96 94.2 2004
12 -03-
109 Yuvraj 95 94.8 2005 26
-02- 108 Deepa 92 96.6 2004
18 k -04-
12
109 Yuvraj 95 94.8 2005
Inserting data in Table -02-
18
Insert Command - (Used for 111 Seema NULL NULL 2005
-08-
inserting data in table) 14
112 NULL NULL 93.2 NULL
Syntax/Example

Inserting full record Updating data in Table


insert into student values (101, “Karan”, 98,
Update Command - (Used for
92.6, “2004-01-23”);
updating data in already existing
insert into student values (103, “Diya”, 89, table)
94.2, “2005-06-30”);
Example
Inserting null values
update student set marks=100 where
insert into student values(111, “Seema”, rollno=112;
null, null, “2005-08-14”);
Output Table after updating Command
Inserting data in specific column
Student
insert into student (rollno, percentage) Rolln Name Mark Percentag Dob
values(112,93.2); o s e
101 Karan 98 92.6 2004
Output table after insert command -01-
23
Student 103 Diya 89 98.4 2005
-06- 26
30 108 Deepa 92 96.6 2004
105 Riya 96 94.2 2004 k -04-
-03- 12
26 109 Yuvraj 95 94.8 2005
108 Deepa 92 96.6 2004 -02-
k -04- 18
12
109 Yuvraj 95 94.8 2005
-02- Displaying data from table
18
111 Seema NULL NULL 2005 Select Commands -(used for
-08-
14 displaying data from
112 NULL 100 93.2 NULL
tables/databases)
deleting data from table Sample table for understanding select
commands
delete command -(use to Student
delete record based on Rollno Name Mark Percentag Dob
(Primar s e
condition) y Key)
101 Karan 98 92.6 2004
Example -01-
23
Delete from student where marks is null; 103 Diya 89 98.4 2005
-06-
Delete from student where name is null; 30
105 Riya 96 94.2 2004
Output table after delete command
-03-
Student 26
Rolln Name Mark Percentag Dob 108 Deepa 92 96.6 2004
o s e k -04-
101 Karan 98 92.6 2004 12
-01- 109 Yuvraj 95 94.8 2005
23 -02-
103 Diya 89 98.4 2005 18
-06- 111 Seema NULL NULL 2005
30 -08-
105 Riya 96 94.2 2004 14
-03- 112 NULL NULL 93.2 NUL
L
112 NULL

Selecting Entire Data


Selecting in specific order
Select * from student;
Select percentage, name, rollno from
Output student;
Rolln Name Mark Percentag Dob Percentage Name Rollno
o s e
92.6 Karan 101
101 Karan 98 92.6 2004 98.4 Diya 103
-01-
94.2 Riya 105
23
96.6 Deepak 108
103 Diya 89 98.4 2005
94.8 Yuvraj 109
-06-
NULL Seema 111
30
93.2 NULL 112
105 Riya 96 94.2 2004
-03-
26
Deepa
Adding constant string in Resultant
108 92 96.6 2004
k -04- Table
12
109 Yuvraj 95 94.8 2005 Select rollno, “has got”, marks, “ and this”,
-02- percentage from student;
18
111 Seema NULL NULL 2005 Rollno has Marks And Percentage
-08- got this
14 101 has 98 and 92.6
112 NULL NULL 93.2 NULL got this
103 has 89 and 98.4
got this
Selecting specific columns 105 has 96 and 94.2
got this
Select rollno, percentage from student; 108 has 92 and 96.6
got this
Output 109 has 95 and 94.8
got this
Rollno Name 111 has NULL and NULL
101 Karan got this
103 Diya 112 has NULL and 93.2
105 Riya got this
108 Deepak
109 Yuvraj
111 Seema Aliasing a table name
Select name as First_Name , marks as Where Clause - (It is used
Final_Marks from student;
with select command and
First_Name Final_Marks
Karan 98 gives result based on a
Diya 89
Riya 96
condition(s) using different
Deepak 92 operators)
Yuvraj 95
Seema NULL Sample table for understand where clause
NULL NULL
Student
Rollno Name Marks City
Selecting distinct (non common 5 Karan 98 Delhi
data) from table 8 Riya 97 Mumbai
10 Diya 98 Delhi
Select distinct marks from student; 15 Yuvraj 96 Agra
20 Deepak 98 Mumbai
Final_Marks
98
89
96
Using operators
92 Syntax/Example (Mathematical)
95
NULL Select * from student

Where city= “Delhi”;


Operators in SQL Rollno Name Marks City
5 Karan 98 Delhi
1. Mathematical Operator (+, -, *,/) 10 Diya 98 Delhi
2. Relational (<, <=, >, >=,<>, =)
3. Logical operator (and , or)
Syntax/Example (Relational)
Simple calculations using operators
Select *from student
Select 7+9-5 as Result;
Where marks =97;
Result
11 Rollno Name Marks City
8 Riya 97 Mumbai
Syntax/Example (Mathematical, Relational Select * from student
and Logical)
Where marks in not null and
Select * from student city=”Mumbai”;

Where city <> “Delhi” and marks>96; Rollno Name Marks City
8 Riya 97 Mumbai
Rollno Name Marks City 20 Deepak 98 Mumbai
8 Riya 97 Mumbai
20 Deepak 98 Mumbai
Selecting data between a
Selecting based on a list Range
of data Between value and value

In and not in operator Example

Example Select * from student

Select * from student Where marks between 98 and 100;

Where city in (“Delhi”, “Pune”); Rollno Name Marks City


5 Karan 98 Delhi
Rollno Name Marks City 10 Diya 98 Delhi
5 Karan 98 Delhi 20 Deepak 98 Mumbai
10 Diya 98 Delhi

Select * from student Selecting data based on


Where city not in (“Delhi”, “Pune”); pattern matching
Rollno Name Marks City Like (% ,_)
8 Riya 97 Mumbai
15 Yuvraj 96 Agra Underscore (_) – Represents a single
20 Deepak 98 Mumbai character

Percent Symbol ( %) – Matches a string

Checking for null values Example

Is null/is not null Select data of students having city name


exactly of 5 characters
Example
Select * from student 15 Yuvraj 96 Agra
8 Riya 97 Mumbai
Where city like “ ______ ”; 5 Karan 98 Delhi
10 Diya 98 Delhi
Rollno Name Marks City
20 Deepak 98 Mumbai
5 Karan 98 Delhi
10 Diya 98 Delhi

Aggregate functions
Example
Count(), max(), min(), sum(), avg()
Select data of students whose name are
starting from “R” Select count(*) as Total from student;

Select * from student Total


5
Where name like “R%”;

Rollno Name Marks City Select count(marks), max(marks),


8 Riya 97 Mumbai min(marks),sum(marks), avg(marks)

From student;
Displaying data in Specific Count( Max( Min(m Sum( Avg(m
marks) marks) arks) marks) arks)
order (Ascending or 5 98 96 487 97.40
Descending) 00

Select * from student


Group by clause -(used
Order by name asc;
with aggregate function
Rollno Name Marks City
20 Deepak 98 Mumbai and grouping data based
10 Diya 98 Delhi
5 Karan 98 Delhi on a particular field)
8 Riya 97 Mumbai
15 Yuvraj 96 Agra Select city, count(*) as Total

From student
Select * from student
Group by city;
Order by name desc;
City Total
Rollno Name Marks City Agra 1
Delhi 2 103 Data Structure
Mumbai 2 105 Computer Networks
102 SQL

Having clause Cartesian product (Cross Join/Cross


Product)
It is used with aggregate function for giving
1. Cardinality = Cardinality * Cardinality
condition.
2. Degree = Degree + Degree
Example

Select city, cout(*) as Total


Example
From student
Select * from student, library;
Group by city
Result
Having count(city)>1;
Student X Library
City Total Id Name Marks Id Book
Delhi 2 101 Karan 90 103 Data
Mumbai 2 Structure
101 Karan 90 105 Computer
Networks
101 Karan 90 102 SQL
102 Deepak 99 103 Data
Joins in SQL Structure
102 Deepak 99 105 Computer
A JOIN is a query that combines tuples from Networks
102 Deepak 99 102 SQL
more than one table.
104 Pooja 97 103 Data
Sample Tables Structure
104 Pooja 97 105 Computer
Student Networks
Id Name Marks 104 Pooja 97 102 SQL
101 Karan 90 105 Diya 97 103 Data
102 Deepak 99 Structure
104 Pooja 97 105 Diya 97 105 Computer
105 Diya 97 Networks
105 Diya 97 102 SQL

Library
Id Book
Equi- Join
Select * from student, library

Where student.id=library.id;

Using alias

Select * from student s, library l

Where s.id=l.id;

Result Table

Student
Id Name Marks Id Book
102 Deepak 99 102 SQL
105 Diya 97 105 Computer
Networks

Natural Join
Select * from student natural join library;

Result Table

Student
Id Name Marks Id Book
102 Deepak 99 102 SQL
105 Diya 97 105 Computer
Networks

You might also like