SQL - Structured query language introduction

S
Smriti JainSelf-Employed, blogger
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
1 von 24

Recomendados

Sql pptSql ppt
Sql pptAnuja Lad
88.2K views68 Folien
SQL BasicsSQL Basics
SQL BasicsHammad Rasheed
40.4K views23 Folien
Introduction to SQLIntroduction to SQL
Introduction to SQLEhsan Hamzei
77.8K views25 Folien
SQL commandsSQL commands
SQL commandsGirdharRatne
8.8K views21 Folien
SQL OverviewSQL Overview
SQL OverviewStewart Rogers
6K views37 Folien

Más contenido relacionado

Was ist angesagt?(20)

SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
Sharad Dubey16.2K views
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia12.6K views
RdbmsRdbms
Rdbms
Muhammad Adeel Rajput15.8K views
SQL Commands SQL Commands
SQL Commands
Sachidananda M H2K views
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR8.3K views
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies31.6K views
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
Dhananjay Goel6.9K views
MysqlMysql
Mysql
TSUBHASHRI1.9K views
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
Shrija Madhu1.1K views
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
Amrit Kaur3.2K views
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
Anusha sivakumar1.5K views
Sql commandsSql commands
Sql commands
Pooja Dixit594 views
SQL ViewsSQL Views
SQL Views
baabtra.com - No. 1 supplier of quality freshers11.5K views
Ms sql-serverMs sql-server
Ms sql-server
Md.Mojibul Hoque9.2K views
 Basic SQL and History Basic SQL and History
Basic SQL and History
SomeshwarMoholkar2.6K views
Data dictionaryData dictionary
Data dictionary
Surbhi Panhalkar34.7K views
Database languageDatabase language
Database language
University of Science and Technology Chitttagong12K views
Sql commandsSql commands
Sql commands
Balakumaran Arunachalam4.2K views
Relational Data Model IntroductionRelational Data Model Introduction
Relational Data Model Introduction
Nishant Munjal13.4K views

Destacado(20)

Introduction to SQLIntroduction to SQL
Introduction to SQL
Ram Kedem24.6K views
Introduction to DatabasesIntroduction to Databases
Introduction to Databases
Ram Kedem19.5K views
SQL : introductionSQL : introduction
SQL : introduction
Shakila Mahjabin7.3K views
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata27.2K views
Normalization,ddl,dml,dclNormalization,ddl,dml,dcl
Normalization,ddl,dml,dcl
baabtra.com - No. 1 supplier of quality freshers1.5K views
Data  manipulation languageData  manipulation language
Data manipulation language
Universitas Bina Darma Palembang360 views
Podcasting in LibrariesPodcasting in Libraries
Podcasting in Libraries
Mark O'English1.3K views
Using Wordpress To Create Your WebsiteUsing Wordpress To Create Your Website
Using Wordpress To Create Your Website
Nicole C. Engard3.3K views
Programming for Babies at your LibraryProgramming for Babies at your Library
Programming for Babies at your Library
Indiana State Library2.6K views
DDL DML sysytemsDDL DML sysytems
DDL DML sysytems
bhujendhar05349 views
Coding as a Practical Library ProgramCoding as a Practical Library Program
Coding as a Practical Library Program
Jennifer Koerber2.4K views
Podcasting for Library InstructionPodcasting for Library Instruction
Podcasting for Library Instruction
Debbie Herman1.3K views
Successfully Using QR Codes in LibrariesSuccessfully Using QR Codes in Libraries
Successfully Using QR Codes in Libraries
Cheryl Burnette6.5K views
Relational Model in dbms & sql databaseRelational Model in dbms & sql database
Relational Model in dbms & sql database
gourav kottawar2.3K views

Similar a SQL - Structured query language introduction

MySQL 指南MySQL 指南
MySQL 指南YUCHENG HU
423 views25 Folien
Unit04 dbmsUnit04 dbms
Unit04 dbmsarnold 7490
1.3K views82 Folien

Similar a SQL - Structured query language introduction(20)

MySQL 指南MySQL 指南
MySQL 指南
YUCHENG HU423 views
My sql advacnce topicsMy sql advacnce topics
My sql advacnce topics
Prabhat gangwar589 views
Unit04 dbmsUnit04 dbms
Unit04 dbms
arnold 74901.3K views
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
Smriti Jain15.3K views
SQL PPT.pptSQL PPT.ppt
SQL PPT.ppt
hemamalinikrishnan231 views
dbms first unitdbms first unit
dbms first unit
Raghu Bright863 views
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
koolkampus5.8K views
Assignment#08Assignment#08
Assignment#08
Sunita Milind Dol1.4K views
Mysql tutorialMysql tutorial
Mysql tutorial
santosh mishra737 views
Mysql tutorialMysql tutorial
Mysql tutorial
santosh mishra328 views
Mysql tutorialMysql tutorial
Mysql tutorial
santosh mishra701 views
Ch4Ch4
Ch4
Welly Dian Astika265 views
ch3ch3
ch3
KITE www.kitecolleges.com717 views
Assignment#04Assignment#04
Assignment#04
Sunita Milind Dol1.4K views
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
anuragmbst1.8K views
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
LPhct213 views

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

Hinweis der Redaktion

  1. 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).
  2. The SQL DML includes a query language based on both the relational algebra and the tuple relational calculus.
  3. 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.
  4. 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.
  5. 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.
  6. By default, the order by clause lists items in ascending order.
  7. 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
  8. 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
  9. 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.