SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
SQL - Structured query language introduction
INTRODUCTION
 SQL (Structured Query Language) is a computer
language aimed to store, manipulate, and retrieve data
stored in relational databases.
 IBM implemented the language, originally called
Sequel, as part of the System R project in the early
1970s..
 The first commercial relational database was released
by Relational Software later becoming oracle.
INTRODUCTION
The SQL language has several parts:
 Data-definition language (DDL). provides
commands for defining relation schemas, deleting
relations, and modifying relation schemas.
 Interactive data-manipulation language (DML). It
includes also commands to insert tuples into, delete
tuples from, and modify tuples in the database.
 View definition. includes commands for defining
views.
 Transaction control. includes commands for
specifying the beginning and ending of transactions.
INTRODUCTION
 Embedded SQL and dynamic SQL. define how SQL
statements can be embedded within general-purpose
programming languages, such as C, C++, Java, PL/I,
Cobol, Pascal, and Fortran.
 Integrity. The SQL DDL includes commands for
specifying integrity constraints that the data stored in
the database must satisfy. Updates that violate integrity
constraints are disallowed.
 Authorization (DCL). The SQL DDL includes
commands for specifying access rights to relations and
views.
SQL - Structured query language introduction
DDL DDL - Data Definition Language:
 Statements used to define the database structure or
schema. Some examples:
 CREATE - to create objects in the database
 ALTER - alters the structure of the database
 DROP - delete objects from the database
 RENAME - rename an object
Schema Definition in SQL
 Create table : Example :
create table branch (branch-name char(15), branch-city
char(30),assets integer,
primary key (branch-name),
check (assets >= 0))
 Delete :
Delete table : drop table r
Delete tuples : Delete from r
 Alter table :
Add Attribute : alter table r add AD
Drop attribute : alter table r drop A
SQL - Structured query language introduction
Data Manipulation Language
DML- Data Manipulation Language:
Statements used for managing data within
schema objects.
 SELECT - retrieve data from the a database
 INSERT - insert data into a table
 UPDATE - updates existing data within a table
 DELETE - deletes all records from a table, the
space for the records remain
 CALL - call a PL/SQL or Java subprogram
Example Setting Schema &
Attributes
 Branch-schema = (branch-name, branch-city,
assets)
 Customer-schema = (customer-name, customer-
street, customer-city)
 Loan-schema = (loan-number, branch-name,
amount)
 Borrower-schema = (customer-name, loan-number)
 Account-schema = (account-number, branch-name,
balance)
 Depositor-schema = (customer-name, account-
number)
Basic SQL Query
 Select Clause :
1. select branch-name from loan Retain Duplicates
2. select distinct branch-name from loan Remove
duplicates
3. select all branch-name from loan Retain Duplicates
 Where Clause :
1. select loan-number from loan where branch-name =
’Perryridge’ and amount > 1200
 From Clause :
select customer-name, borrower.loan-number, amount
from borrower, loan where borrower.loan-number =
loan.loan-number
Basic SQL Query
 Rename Operation :
select customer-name, borrower.loan-number as loan-id,
amount from borrower, loan where borrower.loan-
number = loan.loan-number
 Tuple variables :
select customer-name, T.loan-number, S.amount from
borrower as T, loan as S where T.loan-number = S.loan-
number
Tuple variables are most useful for comparing two tuples
in the same relation.
 String Operations :
select customer-name from customer where customer-
street like ’%Main%’
Basic SQL Query
 Ordering the Display of Tuples :
select distinct customer-name from borrower, loan where
borrower.loan-number = loan.loan-number and branch-
name = ’Perryridge’ order by customer-name
select * from loan order by amount desc, loan-number asc
Set Operations
 Union Operation : find all customers having a loan,
an account, or both
(select customer-name from depositor) union (select
customer-name from borrower) Removes Duplicates
 Intersect Operation : find all customers who have
both a loan and an account
(select distinct customer-name from depositor) intersect
(select distinct customer-name from borrower) Removes
Duplicates
 Except Operation : find all customers who have an
account but no loan
(select distinct customer-name from depositor) except
(select customer-name from borrower)
Aggregate Functions
avg, min, max, sum, count
Example : “Find the average balance for each customer
who lives in Harrison and has at least three accounts.”
select depositor.customer-name, avg (balance)
from depositor, account, customer
where depositor.account-number = account.account-
number and
depositor.customer-name = customer.customer-name and
customer-city = ’Harrison’
group by depositor.customer-name
having count (distinct depositor.account-number) >= 3
Nested Subqueries
 Set Membership : Example : find those customers
who are borrowers from the bank and who appear in
the list of account holders
select distinct customer-name from borrower where
customer-name in (select customer-name from
depositor)
 Set Comparison : Example : Find the names of all
branches that have assets greater than those of at least
one branch located in Brooklyn.
select branch-name from branch where assets > some
(select assets from branch where branch-city =
’Brooklyn’)
>some : greater than at least one member
Views
 Find for each branch the sum of the amounts of all the
loans at the branch.
create view branch-total-loan(branch-name, total-loan) as
select branch-name, sum(amount) from loan groupby
branch-name
Complex Queries
 Derived Relations : Example : “find the maximum
across all branches of the total balance at each branch.”
select max(tot-balance)
from (select branch-name, sum(balance) from account
group by branch-name)
as branch-total (branch-name, tot-balance)
 with Clause : Example : find accounts with the
maximum balance; if there are many accounts with the
same maximum balance, all of them are selected.
with max-balance (value) as
select max(balance) from account
select account-number from account, max-balance
where account.balance = max-balance.value
Modification of the Database
 Deletion :
delete from account where branch-name = ’Perryridge’
 Insertion :
insert into account (account-number, branch-name,
balance) values (’A-9732’, ’Perryridge’, 1200)
 Updates : Example : “Pay 5 percent interest on
accounts whose balance is greater than average”
update account set balance = balance * 1.05
where balance > select avg (balance) from account
Joined Relations
Joined Relations
Joined Relations
Joined Relations
SQL - Structured query language introduction

More Related Content

What's hot (20)

PPTX
DATABASE CONSTRAINTS
sunanditaAnand
 
PPTX
SQL Commands
Sachidananda M H
 
PPTX
Types Of Keys in DBMS
PadamNepal1
 
PPTX
Relational model
Dabbal Singh Mahara
 
PPTX
Relational algebra ppt
GirdharRatne
 
PPTX
Sql commands
Pooja Dixit
 
PPTX
Relational database
Megha Sharma
 
PPTX
joins in database
Sultan Arshad
 
PPT
Types Of Join In Sql Server - Join With Example In Sql Server
programmings guru
 
PPT
Data dictionary
Surbhi Panhalkar
 
PPT
PL/SQL Introduction and Concepts
Bharat Kalia
 
PPTX
SQL JOIN
Ritwik Das
 
PPTX
All data models in dbms
Naresh Kumar
 
ODP
Ms sql-server
Md.Mojibul Hoque
 
PPT
Databases: Normalisation
Damian T. Gordon
 
PPTX
SQL Joins.pptx
Ankit Rai
 
PPTX
DBMS: Types of keys
Bharati Ugale
 
PPT
1 - Introduction to PL/SQL
rehaniltifat
 
DATABASE CONSTRAINTS
sunanditaAnand
 
SQL Commands
Sachidananda M H
 
Types Of Keys in DBMS
PadamNepal1
 
Relational model
Dabbal Singh Mahara
 
Relational algebra ppt
GirdharRatne
 
Sql commands
Pooja Dixit
 
Relational database
Megha Sharma
 
joins in database
Sultan Arshad
 
Types Of Join In Sql Server - Join With Example In Sql Server
programmings guru
 
Data dictionary
Surbhi Panhalkar
 
PL/SQL Introduction and Concepts
Bharat Kalia
 
SQL JOIN
Ritwik Das
 
All data models in dbms
Naresh Kumar
 
Ms sql-server
Md.Mojibul Hoque
 
Databases: Normalisation
Damian T. Gordon
 
SQL Joins.pptx
Ankit Rai
 
DBMS: Types of keys
Bharati Ugale
 
1 - Introduction to PL/SQL
rehaniltifat
 

Viewers also liked (20)

PDF
Introduction to SQL
Ram Kedem
 
PDF
Introduction to Databases
Ram Kedem
 
PPT
SQL : introduction
Shakila Mahjabin
 
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
PPT
Data manipulation language
Universitas Bina Darma Palembang
 
PDF
Part 7 ddl dan dml lant..retriving data up
Denny Yahya
 
PPT
Podcasting in Libraries
Mark O'English
 
PDF
Using Wordpress To Create Your Website
Nicole C. Engard
 
PPTX
Digital Disruption: A Librarian's perspective
Jane Cowell
 
PPT
Programming for Babies at your Library
Indiana State Library
 
PPTX
Fundamentals of database system - Database System Concepts and Architecture
Mustafa Kamel Mohammadi
 
PPT
DDL DML sysytems
bhujendhar05
 
PDF
Coding as a Practical Library Program
Jennifer Koerber
 
DOCX
retrieving data using SQL statements
Arun Nair
 
PPT
Podcasting for Library Instruction
Debbie Herman
 
PPTX
12 Things You Should Know About Library Guides
this_shanti
 
PPTX
Successfully Using QR Codes in Libraries
Cheryl Burnette
 
PPTX
Sql server ___________session_19(triggers)
Ehtisham Ali
 
Introduction to SQL
Ram Kedem
 
Introduction to Databases
Ram Kedem
 
SQL : introduction
Shakila Mahjabin
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
SQL Tutorial - Basic Commands
1keydata
 
Data manipulation language
Universitas Bina Darma Palembang
 
Part 7 ddl dan dml lant..retriving data up
Denny Yahya
 
Podcasting in Libraries
Mark O'English
 
Using Wordpress To Create Your Website
Nicole C. Engard
 
Digital Disruption: A Librarian's perspective
Jane Cowell
 
Programming for Babies at your Library
Indiana State Library
 
Fundamentals of database system - Database System Concepts and Architecture
Mustafa Kamel Mohammadi
 
DDL DML sysytems
bhujendhar05
 
Coding as a Practical Library Program
Jennifer Koerber
 
retrieving data using SQL statements
Arun Nair
 
Podcasting for Library Instruction
Debbie Herman
 
12 Things You Should Know About Library Guides
this_shanti
 
Successfully Using QR Codes in Libraries
Cheryl Burnette
 
Sql server ___________session_19(triggers)
Ehtisham Ali
 
Ad

Similar to SQL - Structured query language introduction (20)

PPT
Tutorial on MySQl and its basic concepts
anishacotta2
 
PDF
MySQL 指南
YUCHENG HU
 
PPT
My sql advacnce topics
Prabhat gangwar
 
PPT
Unit04 dbms
arnold 7490
 
PPTX
Database : Relational Data Model
Smriti Jain
 
PDF
Sm relationaldatamodel-150423084157-conversion-gate01
Ankit Dubey
 
PDF
Sm relationaldatamodel-150423084157-conversion-gate01
Ankit Dubey
 
PPT
SQL PPT.ppt
hemamalinikrishnan2
 
DOCX
dbms first unit
Raghu Bright
 
PPT
chapter4 contains details about sql queries.ppt
juhishrivastava25
 
PPT
4. SQL in DBMS
koolkampus
 
PPT
Mysql classes in navi-mumbai,mysql course-provider-in-navi-mumbai
anshkhurana01
 
PPT
sql- introduction-notmine- uploading share
MariaLuisaCarlos
 
PDF
Assignment#08
Sunita Milind Dol
 
PPT
Mysql tutorial
santosh mishra
 
PPT
Mysql tutorial
santosh mishra
 
PPT
Mysql tutorial
santosh mishra
 
PPT
5. Other Relational Languages in DBMS
koolkampus
 
PPT
Data Warehouse , Data Cube Computation
sit20ad004
 
Tutorial on MySQl and its basic concepts
anishacotta2
 
MySQL 指南
YUCHENG HU
 
My sql advacnce topics
Prabhat gangwar
 
Unit04 dbms
arnold 7490
 
Database : Relational Data Model
Smriti Jain
 
Sm relationaldatamodel-150423084157-conversion-gate01
Ankit Dubey
 
Sm relationaldatamodel-150423084157-conversion-gate01
Ankit Dubey
 
SQL PPT.ppt
hemamalinikrishnan2
 
dbms first unit
Raghu Bright
 
chapter4 contains details about sql queries.ppt
juhishrivastava25
 
4. SQL in DBMS
koolkampus
 
Mysql classes in navi-mumbai,mysql course-provider-in-navi-mumbai
anshkhurana01
 
sql- introduction-notmine- uploading share
MariaLuisaCarlos
 
Assignment#08
Sunita Milind Dol
 
Mysql tutorial
santosh mishra
 
Mysql tutorial
santosh mishra
 
Mysql tutorial
santosh mishra
 
5. Other Relational Languages in DBMS
koolkampus
 
Data Warehouse , Data Cube Computation
sit20ad004
 
Ad

Recently uploaded (20)

PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Digital water marking system project report
Kamal Acharya
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Design Thinking basics for Engineers.pdf
CMR University
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 

SQL - Structured query language introduction

  • 2. INTRODUCTION  SQL (Structured Query Language) is a computer language aimed to store, manipulate, and retrieve data stored in relational databases.  IBM implemented the language, originally called Sequel, as part of the System R project in the early 1970s..  The first commercial relational database was released by Relational Software later becoming oracle.
  • 3. INTRODUCTION The SQL language has several parts:  Data-definition language (DDL). provides commands for defining relation schemas, deleting relations, and modifying relation schemas.  Interactive data-manipulation language (DML). It includes also commands to insert tuples into, delete tuples from, and modify tuples in the database.  View definition. includes commands for defining views.  Transaction control. includes commands for specifying the beginning and ending of transactions.
  • 4. INTRODUCTION  Embedded SQL and dynamic SQL. define how SQL statements can be embedded within general-purpose programming languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran.  Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in the database must satisfy. Updates that violate integrity constraints are disallowed.  Authorization (DCL). The SQL DDL includes commands for specifying access rights to relations and views.
  • 6. DDL DDL - Data Definition Language:  Statements used to define the database structure or schema. Some examples:  CREATE - to create objects in the database  ALTER - alters the structure of the database  DROP - delete objects from the database  RENAME - rename an object
  • 7. Schema Definition in SQL  Create table : Example : create table branch (branch-name char(15), branch-city char(30),assets integer, primary key (branch-name), check (assets >= 0))  Delete : Delete table : drop table r Delete tuples : Delete from r  Alter table : Add Attribute : alter table r add AD Drop attribute : alter table r drop A
  • 9. Data Manipulation Language DML- Data Manipulation Language: Statements used for managing data within schema objects.  SELECT - retrieve data from the a database  INSERT - insert data into a table  UPDATE - updates existing data within a table  DELETE - deletes all records from a table, the space for the records remain  CALL - call a PL/SQL or Java subprogram
  • 10. Example Setting Schema & Attributes  Branch-schema = (branch-name, branch-city, assets)  Customer-schema = (customer-name, customer- street, customer-city)  Loan-schema = (loan-number, branch-name, amount)  Borrower-schema = (customer-name, loan-number)  Account-schema = (account-number, branch-name, balance)  Depositor-schema = (customer-name, account- number)
  • 11. Basic SQL Query  Select Clause : 1. select branch-name from loan Retain Duplicates 2. select distinct branch-name from loan Remove duplicates 3. select all branch-name from loan Retain Duplicates  Where Clause : 1. select loan-number from loan where branch-name = ’Perryridge’ and amount > 1200  From Clause : select customer-name, borrower.loan-number, amount from borrower, loan where borrower.loan-number = loan.loan-number
  • 12. Basic SQL Query  Rename Operation : select customer-name, borrower.loan-number as loan-id, amount from borrower, loan where borrower.loan- number = loan.loan-number  Tuple variables : select customer-name, T.loan-number, S.amount from borrower as T, loan as S where T.loan-number = S.loan- number Tuple variables are most useful for comparing two tuples in the same relation.  String Operations : select customer-name from customer where customer- street like ’%Main%’
  • 13. Basic SQL Query  Ordering the Display of Tuples : select distinct customer-name from borrower, loan where borrower.loan-number = loan.loan-number and branch- name = ’Perryridge’ order by customer-name select * from loan order by amount desc, loan-number asc
  • 14. Set Operations  Union Operation : find all customers having a loan, an account, or both (select customer-name from depositor) union (select customer-name from borrower) Removes Duplicates  Intersect Operation : find all customers who have both a loan and an account (select distinct customer-name from depositor) intersect (select distinct customer-name from borrower) Removes Duplicates  Except Operation : find all customers who have an account but no loan (select distinct customer-name from depositor) except (select customer-name from borrower)
  • 15. Aggregate Functions avg, min, max, sum, count Example : “Find the average balance for each customer who lives in Harrison and has at least three accounts.” select depositor.customer-name, avg (balance) from depositor, account, customer where depositor.account-number = account.account- number and depositor.customer-name = customer.customer-name and customer-city = ’Harrison’ group by depositor.customer-name having count (distinct depositor.account-number) >= 3
  • 16. Nested Subqueries  Set Membership : Example : find those customers who are borrowers from the bank and who appear in the list of account holders select distinct customer-name from borrower where customer-name in (select customer-name from depositor)  Set Comparison : Example : Find the names of all branches that have assets greater than those of at least one branch located in Brooklyn. select branch-name from branch where assets > some (select assets from branch where branch-city = ’Brooklyn’) >some : greater than at least one member
  • 17. Views  Find for each branch the sum of the amounts of all the loans at the branch. create view branch-total-loan(branch-name, total-loan) as select branch-name, sum(amount) from loan groupby branch-name
  • 18. Complex Queries  Derived Relations : Example : “find the maximum across all branches of the total balance at each branch.” select max(tot-balance) from (select branch-name, sum(balance) from account group by branch-name) as branch-total (branch-name, tot-balance)  with Clause : Example : find accounts with the maximum balance; if there are many accounts with the same maximum balance, all of them are selected. with max-balance (value) as select max(balance) from account select account-number from account, max-balance where account.balance = max-balance.value
  • 19. Modification of the Database  Deletion : delete from account where branch-name = ’Perryridge’  Insertion : insert into account (account-number, branch-name, balance) values (’A-9732’, ’Perryridge’, 1200)  Updates : Example : “Pay 5 percent interest on accounts whose balance is greater than average” update account set balance = balance * 1.05 where balance > select avg (balance) from account

Editor's Notes

  • #3: IBM developed the original version of SQL at its San Jose Research Laboratory (now the Almaden Research Center).The Sequel language has evolved since then, and its name has changed to SQL (Structured Query Language).
  • #4: The SQL DML includes a query language based on both the relational algebra and the tuple relational calculus.
  • #8: where r is the name of an existing relation, A is the name of the attribute to be added, and D is the domain of the added attribute.
  • #12: Similarly, we can use the not between comparison operator. SQL uses the logical connectives and, or, and not—rather than the mathematical symbols ∧, ∨, and ¬ —in the where clause. The from clause by itself defines a Cartesian product of the relations in the clause. Notice that SQL uses the notation relation-name.attribute-name, as does the relational algebra, to avoid ambiguity in cases where an attribute appears in the schema of more than one relation. We could have written borrower.customer-name instead of customername in the select clause. However, since the attribute customer-name appears in only one of the relations named in the from clause, there is no ambiguity when we write customer-name.
  • #13: SQL provides a mechanism for renaming both relations and attributes. Ex. Find the names of all branches that have assets greater than those of at least one branch located in Brooklyn. select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and S.branch-city = ’Brooklyn’ Percent (%): The % character matches any substring. Underscore ( _): The _ character matches any character.
  • #14: By default, the order by clause lists items in ascending order.
  • #15: If we want to retain all duplicates, we must write union all in place of union Opposites Union : union all Intersect : intersect all Except : except all
  • #16: Group by clause: Tuples with the same value on all attributes in the group by clause are placed in one group. Having clause : state a condition that applies to groups rather than to tuples. Count clause : to count the number of tuples in a relation. priority Where clause>having clause
  • #17: Opposites : in : not in Alternate way to write second query : select distinct T.branch-name from branch as T, branch as S where T.assets > S.assets and S.branch-city = ’Brooklyn’ SQL also allows < some, <= some, >= some, = some, and<> some comparisons. As an exercise, verify that = some is identical to in, whereas <> some is not the same as not in.