Simple SQL Queries (3)
[Pages:18]Simple SQL Queries (3)
What Have We Learned about SQL?
? Basic SELECT-FROM-WHERE structure ? Selecting from multiple tables ? String operations ? Set operations ? Aggregate functions and group-by queries ? Null values
CMPT 354: Database I -- Simple SQL (3)
2
Nested Subqueries
? When a select-from-where expression is insufficient to express a (complex) query, a subquery as a select-from-where expression can be nested within another query
? Common use of subqueries
? Test for set membership ? Set comparisons ? Set cardinality
CMPT 354: Database I -- Simple SQL (3)
3
Set Membership
? Find all customers who have both an account and a loan at the bank
select distinct customer_name from borrower
where customer_name in
(select customer_name from depositor )
? Find all customers who have a loan at the bank but do not have an account at the bank
select distinct customer_name from borrower
where customer_name not in
(select customer_name from depositor)
? Can be written without sub-queries (how?)
CMPT 354: Database I -- Simple SQL (3)
4
A More Complex Example
? Find all customers who have both an account and a loan at the Perryridge branch
select distinct customer_name from borrower, loan where borrower.loan_number = loan.loan_number and
branch_name = `Perryridge' and (branch_name, customer_name ) in
(select branch_name, customer_name from depositor, account where depositor.account_number =
account.account_number )
? Can you write a simpler query?
CMPT 354: Database I -- Simple SQL (3)
5
Set Comparison
? Find all branches that have greater assets than some 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'
? Using "> some" clause
select branch_name from branch where assets > some
(select assets from branch where branch_city = `Brooklyn')
CMPT 354: Database I -- Simple SQL (3)
6
All Clause
? Find the names of all branches that have greater assets than all branches located in Brooklyn
select branch_name from branch where assets > all
(select assets from branch where branch_city = `Brooklyn')
CMPT 354: Database I -- Simple SQL (3)
7
Test for Empty Relations
? The exists construct returns the value true if the argument subquery is nonempty
? exists r r ? ? not exists r r = ?
CMPT 354: Database I -- Simple SQL (3)
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- sql tutorialspoint
- manage comprehensive security best practice guide
- sql injection cheat sheet acunetix
- pl sql user s guide and reference 10g release 2 10 2
- t sql fundamentals third edition
- sql quick guide tutorialspoint
- how to write a great case statement u s epa web server
- sql server database coding standards and guidelines
- simple sql queries 3
Related searches
- simple 3 year old crafts
- ecclesiastes 3 explained in simple form
- sql connection string sql user
- azure sql vs azure sql database
- azure sql vs sql databases
- azure sql managed instance vs sql db
- db2 queries tutorial
- example of 3 simple sentences
- hive queries examples
- writing sql queries for dummies
- microsoft access queries criteria
- microsoft access queries formulas