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

PPTX
Introduction to SQL
PPT
Sql ppt
PPTX
Sql queries presentation
PPTX
SQL commands
PPTX
introdution to SQL and SQL functions
PPTX
Introduction to Database
PPTX
SQL Queries Information
PDF
Data structure ppt
Introduction to SQL
Sql ppt
Sql queries presentation
SQL commands
introdution to SQL and SQL functions
Introduction to Database
SQL Queries Information
Data structure ppt

What's hot (20)

PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
PPT
MYSQL.ppt
PPTX
Er model ppt
PPT
Entity Relationship Diagram
PPT
1 - Introduction to PL/SQL
PPTX
SQL(DDL & DML)
PPTX
DBMS Keys
PPT
1. Introduction to DBMS
PPTX
Components and Advantages of DBMS
PPT
PL/SQL Introduction and Concepts
PPT
Architecture of .net framework
PPTX
Relational Data Model Introduction
PPTX
SQL, Embedded SQL, Dynamic SQL and SQLJ
PPTX
Procedure and Functions in pl/sql
PPT
Advanced sql
PPTX
Types Of Keys in DBMS
PPTX
PL/SQL Fundamentals I
PPT
Object Oriented Database Management System
PPTX
Aggregate function
PPTX
Sql commands
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
MYSQL.ppt
Er model ppt
Entity Relationship Diagram
1 - Introduction to PL/SQL
SQL(DDL & DML)
DBMS Keys
1. Introduction to DBMS
Components and Advantages of DBMS
PL/SQL Introduction and Concepts
Architecture of .net framework
Relational Data Model Introduction
SQL, Embedded SQL, Dynamic SQL and SQLJ
Procedure and Functions in pl/sql
Advanced sql
Types Of Keys in DBMS
PL/SQL Fundamentals I
Object Oriented Database Management System
Aggregate function
Sql commands
Ad

Viewers also liked (20)

PDF
Introduction to SQL
PDF
Introduction to Databases
PPT
SQL : introduction
PPTX
SQL Basics
PPT
SQL Tutorial - Basic Commands
PPT
Data manipulation language
PDF
Part 7 ddl dan dml lant..retriving data up
PPT
Podcasting in Libraries
PDF
Using Wordpress To Create Your Website
PPTX
Digital Disruption: A Librarian's perspective
PPT
Programming for Babies at your Library
PPTX
Fundamentals of database system - Database System Concepts and Architecture
PPT
DDL DML sysytems
PDF
Coding as a Practical Library Program
DOCX
retrieving data using SQL statements
PPT
Podcasting for Library Instruction
PPTX
12 Things You Should Know About Library Guides
PPTX
Successfully Using QR Codes in Libraries
PPTX
Sql server ___________session_19(triggers)
Introduction to SQL
Introduction to Databases
SQL : introduction
SQL Basics
SQL Tutorial - Basic Commands
Data manipulation language
Part 7 ddl dan dml lant..retriving data up
Podcasting in Libraries
Using Wordpress To Create Your Website
Digital Disruption: A Librarian's perspective
Programming for Babies at your Library
Fundamentals of database system - Database System Concepts and Architecture
DDL DML sysytems
Coding as a Practical Library Program
retrieving data using SQL statements
Podcasting for Library Instruction
12 Things You Should Know About Library Guides
Successfully Using QR Codes in Libraries
Sql server ___________session_19(triggers)
Ad

Similar to SQL - Structured query language introduction (20)

PPT
Tutorial on MySQl and its basic concepts
PDF
MySQL 指南
PPT
mysql_tutorial dan penjelasan rinci .ppt
PPT
My sql advacnce topics
PPT
Unit04 dbms
PDF
Sm relationaldatamodel-150423084157-conversion-gate01
PPTX
Database : Relational Data Model
PDF
Sm relationaldatamodel-150423084157-conversion-gate01
PPT
SQL PPT.ppt
DOCX
dbms first unit
PPT
chapter4 contains details about sql queries.ppt
PPT
4. SQL in DBMS
PPT
Mysql classes in navi-mumbai,mysql course-provider-in-navi-mumbai
PPT
sql- introduction-notmine- uploading share
PDF
Assignment#08
PPT
Mysql tutorial
PPT
Mysql tutorial
PPT
Mysql tutorial
PPT
5. Other Relational Languages in DBMS
Tutorial on MySQl and its basic concepts
MySQL 指南
mysql_tutorial dan penjelasan rinci .ppt
My sql advacnce topics
Unit04 dbms
Sm relationaldatamodel-150423084157-conversion-gate01
Database : Relational Data Model
Sm relationaldatamodel-150423084157-conversion-gate01
SQL PPT.ppt
dbms first unit
chapter4 contains details about sql queries.ppt
4. SQL in DBMS
Mysql classes in navi-mumbai,mysql course-provider-in-navi-mumbai
sql- introduction-notmine- uploading share
Assignment#08
Mysql tutorial
Mysql tutorial
Mysql tutorial
5. Other Relational Languages in DBMS

Recently uploaded (20)

PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
AgentX UiPath Community Webinar series - Delhi
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Practice Questions on recent development part 1.pptx
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PDF
Queuing formulas to evaluate throughputs and servers
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
anatomy of limbus and anterior chamber .pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Model Code of Practice - Construction Work - 21102022 .pdf
AgentX UiPath Community Webinar series - Delhi
CH1 Production IntroductoryConcepts.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Geodesy 1.pptx...............................................
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Practice Questions on recent development part 1.pptx
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Queuing formulas to evaluate throughputs and servers
Embodied AI: Ushering in the Next Era of Intelligent Systems
anatomy of limbus and anterior chamber .pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
OOP with Java - Java Introduction (Basics)
Simulation of electric circuit laws using tinkercad.pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Structs to JSON How Go Powers REST APIs.pdf
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx

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.