Professional Documents
Culture Documents
Database Unit II
Database Unit II
• Data type
• Each column in the first table must be the same as
the data type of the corresponding column in the
second table.
• Data width and column name can differ
• MIN
• MAX
• AVERAGE
• SUM
• COUNT
Aggregate function - MIN
• Count(*) = No of rows
• Count(ColumnName) = No. of rows that do not
have NULL Value
Aggregate function- count
• select *
from table_name
where test-expression comparison
operator (subquery)
Sample Queries
• Find the name of the branch with the
smallest number of assets.
SELECT
branch_name FROM
branch WHERE
assets =
(SELECT
MIN(assets)
Sample Queries
• Find the names of all branches that have assets
greater than atleast one branch located in
Brooklyn.
select
branch_name from
branch
where assets >
(select min(assets)
from branch
where
branch_city =
Set Membership Tests
• Can use IN (...) and NOT IN (...) for set
membership tests
• IN (...) and NOT IN (...) support subqueries that
return multiple columns (!!!)
• The in connective, where the set is a collection of
values produced by a select clause.
• The not in connective tests for the absence of
set membership.
• Select *
from table_name
where test-expression [NOT] IN (Constan1,
constant2,
….)
Sample Queries
• 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) ;
Sample Queries
• Find all customers who do 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) ;
Sample Queries
• Find the details of the largest loan at each
branch, including the branch name and the
amount of the loan
SELECT *
FROM loan
WHERE (branch_name, amount) IN (
SELECT branch_name,
MAX(amount) FROM loan
GROUP BY branch_name);
Empty-Set Tests
• Can test whether or not a subquery generates any results
at all
• Can use EXISTS (...) and NOT EXISTS (...)
• is a test for a non-empty set and returns either TRUE
or FALSE.
• EXISTS is TRUE if the Table subquery returns at least one
row; otherwise it is FALSE.
• NOT EXISTS is TRUE if the Table subquery returns zero
rows; otherwise it is FALSE.
• SELECT column_1
FROM
Table_1
Sample Queries
• Find all customers who have both an account and
a loan at the bank
SELECT DISTINCT
customer_name FROM depositor d
WHERE EXISTS
(SELECT *
FROM borrower b
WHERE b.customer_name =
d.customer_name);
Sample Queries
• Find customers with an account but not a
loan.
SELECT DISTINCT
customer_name FROM depositor d
WHERE NOT EXISTS
(SELECT *
FROM borrower b
WHERE b.customer_name =
d.customer_name);
Set Comparison Tests
• Can compare a value to a set of values
¤ Is a value larger/smaller/etc. than some value in the set?
• SELECT *
FROM table_name
WHERE attribute comparison_op SOME (subquery);
¤ Can use any comparison operation
= SOME is same as IN
¤ ANY is a synonym for SOME
• Can also compare a value with
all values in a set
¤ Use ALL instead of SOME
ALL quantified comparison
• SELECT *
FROM table_name
WHERE scalar_expression comparison_operator ALL
(subquery);
• scalar_expression : that evaluates to a single value
• comparison_operator may be any one of:
= or > or < or >= or <= or <>.
• ALL returns TRUE if the Table subquery returns zero
rows or if the comparison operator returns TRUE for
every row returned by the Table subquery.
• ALL returns FALSE if the comparison operator returns
FALSE for at least one row returned by the Table
subquery
ALL quantified comparison
• ... WHERE 1000.00 >
ALL (SELECT column_1 FROM Table_1) ...
• If TABLE_1 contains the values {100.00, 200.00, 300.00}, the
expression is TRUE: all of TABLE_1’s values are less than 1000.00.
• If TABLE_1 contains the values {100.00, 2000.00, 300.00},
the expression is FALSE: one of TABLE_1’s values is greater
than 1000.00.
• If TABLE_1 contains the values {1000.00, 200.00, 300.00},
the expression is FALSE too: one of TABLE_1’s values is
equal to 1000.00.
• If TABLE_1 contains no values, the expression is TRUE: when
the set is empty, ALL is TRUE.
• If TABLE_1 contains the values {100.00, NULL, 300.00}, the
expression is UNKNOWN: when NULLs are involved, ALL
ANY or SOME quantified comparison
• SELECT *
FROM table_name
WHERE scalar_expression comparison_operator ANY/ SOME
(subquery);
• scalar_expression : that evaluates to a single value
• comparison_operator may be any one of:
= or > or < or >= or <= or <>.
• SOME and ANY are synonyms.
• They return TRUE if the comparison operator returns
TRUE for at least one row returned by the Table subquery.
• They return FALSE if the Table subquery returns zero rows or
if the comparison operator returns FALSE for every row
returned by the Table subquery.
ANY or SOME quantified comparison
• ... WHERE 1000.00 >
ANY (SELECT column_1 FROM Table_1) ...
• If TABLE_1 contains the values {100.00, 200.00, 300.00}, the
expression is TRUE: all of TABLE_1’s values are less than 1000.00.
• If TABLE_1 contains the values {100.00, 2000.00, 300.00}, the
expression is TRUE too: at least some of TABLE_1’s values are
less than 1000.00.
• If TABLE_1 contains no values, the expression is FALSE: when the
set is empty, ANY is FALSE.
• If TABLE_1 contains the values {1000.00, 2000.00, 3000.00}, the
expression is FALSE too: all of TABLE_1’s values are greater
than or equal to 1000.00.
• If TABLE_1 contains the values {100.00, NULL, 300.00}, the
expression is are UNKNOWN: when NULLs involved, ANY
Sample Queries
• 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
f
r
o
Sample Queries
• Find the names of all branches that have an asset
value greater than that of each branch in
Brooklyn.
select
branch_name from
branch
where assets >
all(select assets
f
r
o
Views
• A view is a kind of “virtual table”
• Views are tables whose contents are taken or derived from
other tables.
• To the user, the view appears like a table with columns and
rows
• But in reality, the view doesn’t exists in the database as a
stored set of values
• The rows and columns that we find inside a view are
actually the results generated by a query that defines
the view
Views
• Syntax to Create a view:
create view view_name( column1, column2, ……. ) as Query
SELECT MAX(tot_bal) AS
large_bal FROM branchwithbal;