26
Mrs. Sunita M Dol, CSE Dept SQL Manual Page 1 Basic Structure of SQL Queries SQL is based on set and relational operations with certain modifications and enhancements. A typical SQL query has the form: select A 1 , A 2 , ..., A n from r 1 , r 2 , ..., r m where P A i represents an attribute, R i represents a relation and P is a predicate. The SELECT Clause The select clause list the attributes desired in the result of a query. SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. The keyword all specifies that duplicates not be removed. An asterisk in the select clause denotes “all attributes”. The select clause can contain arithmetic expressions involving the operation, +, , *, and /, and operating on constants or attributes of tuples. The WHERE Clause The where clause specifies conditions that the result must satisfy. Comparison results can be combined using the logical connectives and, or, and not. Comparisons can be applied to results of arithmetic expressions. SQL includes a between comparison operator. The FROM Clause The from clause lists the relations involved in the query. List the tuples of borrower relation. SQL> select * from borrower; CUSTOMER_NAME LOAN_NUMBE -------------------- ---------- Adams L-16 Curry L-93 Hayes L-15 Jones L-17 Smith L-11 Smith L-23 Williams L-17 7 rows selected.

4. Basic Structure of SQL Queries

Embed Size (px)

Citation preview

Page 1: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 1

Basic Structure of SQL Queries SQL is based on set and relational operations with certain modifications and enhancements. A typical SQL

query has the form:

select A1, A2, ..., An

from r1, r2, ..., rm

where P

– Ai represents an attribute,

– Ri represents a relation and

– P is a predicate.

The SELECT Clause The select clause list the attributes desired in the result of a query. SQL allows duplicates in relations as well

as in query results. To force the elimination of duplicates, insert the keyword distinct after select. The

keyword all specifies that duplicates not be removed. An asterisk in the select clause denotes “all

attributes”. The select clause can contain arithmetic expressions involving the operation, +, –, *, and /, and

operating on constants or attributes of tuples.

The WHERE Clause The where clause specifies conditions that the result must satisfy. Comparison results can be combined

using the logical connectives and, or, and not. Comparisons can be applied to results of arithmetic

expressions. SQL includes a between comparison operator.

The FROM Clause The from clause lists the relations involved in the query.

List the tuples of borrower relation.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

Page 2: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 2

Find the names of all branches in the loan relation

SQL> select branch_name from loan;

BRANCH_NAME

---------------

Round Hill

Downtown

Perryridge

Perryridge

Downtown

Redwood

Mianus

7 rows selected.

Find the distinct name of all branches in the loan relation

SQL> select distinct branch_name from loan;

BRANCH_NAME

---------------

Perryridge

Round Hill

Downtown

Mianus

Redwood

Find the names of all branches in the loan relation

SQL> select all branch_name from loan;

BRANCH_NAME

---------------

Round Hill

Downtown

Perryridge

Perryridge

Downtown

Redwood

Mianus

7 rows selected.

List the tuples of loan relation with amount multiplied by 100.

SQL> select * from loan;

Page 3: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 3

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select loan_number,branch_name,amount*100 from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT*100

---------- --------------- ----------

L-11 Round Hill 90000

L-14 Downtown 150000

L-15 Perryridge 150000

L-16 Perryridge 130000

L-17 Downtown 100000

L-23 Redwood 200000

L-93 Mianus 50000

7 rows selected.

Find all loan numbers for the loans made at Perryridge branch with amount greater than $1200.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select loan_number from loan where branch_name='Perryridge' and amount>1200;

LOAN_NUMBE

Page 4: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 4

----------

L-15

L-16

Find the loan number of those loans with loan amount between 900 and 1500.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select loan_number from loan where amount between 900 and 1500;

LOAN_NUMBE

----------

L-11

L-14

L-15

L-16

L-17

SQL> select loan_number from loan where amount>=900 and amount<=1500;

LOAN_NUMBE

----------

L-11

L-14

L-15

L-16

L-17

For all customers who have a loan from the bank,find their names,loan numbers and loan amount.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBER

-------------------- ----------

Page 5: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 5

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select customer_name,borrower.loan_number,amount

2 from borrower,loan

3 where borrower.loan_number=loan.loan_number;

CUSTOMER_NAME LOAN_NUMBER AMOUNT

-------------------- ---------- ----------

Smith L-11 900

Hayes L-15 1500

Adams L-16 1300

Williams L-17 1000

Jones L-17 1000

Smith L-23 2000

Curry L-93 500

7 rows selected.

Page 6: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 6

Cartesian product of borrower and loan.

SQL> select customer_name,borrower.loan_number,

2 from borrower,loan;

CUSTOMER_NAME LOAN_NUMBER AMOUNT

-------------------- ---------- ----------

Adams L-16 900

Curry L-93 900

Hayes L-15 900

Jones L-17 900

Smith L-11 900

Smith L-23 900

Williams L-17 900

Adams L-16 1500

Curry L-93 1500

Hayes L-15 1500

Jones L-17 1500

CUSTOMER_NAME LOAN_NUMBER AMOUNT

-------------------- ---------- ----------

Smith L-11 1500

Smith L-23 1500

Williams L-17 1500

Adams L-16 1500

Curry L-93 1500

Hayes L-15 1500

Jones L-17 1500

Smith L-11 1500

Smith L-23 1500

Williams L-17 1500

Adams L-16 1300

CUSTOMER_NAME LOAN_NUMBE AMOUNT

-------------------- ---------- ----------

Curry L-93 1300

Hayes L-15 1300

Jones L-17 1300

Smith L-11 1300

Smith L-23 1300

Williams L-17 1300

Adams L-16 1000

Curry L-93 1000

Hayes L-15 1000

Jones L-17 1000

Page 7: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 7

Smith L-11 1000

CUSTOMER_NAME LOAN_NUMBE AMOUNT

-------------------- ---------- ----------

Smith L-23 1000

Williams L-17 1000

Adams L-16 2000

Curry L-93 2000

Hayes L-15 2000

Jones L-17 2000

Smith L-11 2000

Smith L-23 2000

Williams L-17 2000

Adams L-16 500

Curry L-93 500

CUSTOMER_NAME LOAN_NUMBE AMOUNT

-------------------- ---------- ----------

Hayes L-15 500

Jones L-17 500

Smith L-11 500

Smith L-23 500

Williams L-17 500

49 rows selected.

Find the customer names, loan numbers and loan amounts for all loans at the Perryridge branch.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

Page 8: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 8

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select customer_name,borrower.loan_number,amount

2 from borrower,loan

3 where borrower.loan_number=loan.loan_number

4 and branch_name='Perryridge';

CUSTOMER_NAME LOAN_NUMBE AMOUNT

-------------------- ---------- ----------

Adams L-16 1300

Hayes L-15 1500

Page 9: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 9

The RENAME Operation The SQL allows renaming relations and attributes using the as clause:

old-name as new-name

For all customers who have a loan from the bank, find their names, loan numbers renamed as loan_id

and loan amount.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select customer_name,borrower.loan_number as loan_id,amount

2 from borrower,loan

3 where borrower.loan_number=loan.loan_number;

CUSTOMER_NAME LOAN_ID AMOUNT

-------------------- ---------- ----------

Smith L-11 900

Hayes L-15 1500

Adams L-16 1300

Page 10: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 10

Williams L-17 1000

Jones L-17 1000

Smith L-23 2000

Curry L-93 500

7 rows selected.

Page 11: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 11

Tuple Variables Tuple variables are defined in the from clause via the use of the as clause. Keyword as is optional and may

be omitted

borrower as T ≡ borrower T

For all customers who have a loan from the bank, find their names, loan numbers and loan amount.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

SQL> select customer_name,T.loan_number,S.amount

2 from borrower as T,loan as S

3 where T.loan_number=S.loan_number;

from borrower as T,loan as S

*

ERROR at line 2:

ORA-00933: SQL command not properly ended

SQL> select customer_name,T.loan_number,S.amount

2 from borrower T,loan S

3 where T.loan_number=S.loan_number;

Page 12: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 12

CUSTOMER_NAME LOAN_NUMBE AMOUNT

-------------------- ---------- ----------

Smith L-11 900

Hayes L-15 1500

Adams L-16 1300

Williams L-17 1000

Jones L-17 1000

Smith L-23 2000

Curry L-93 500

7 rows selected.

Find the names of all branches that have assets greater than at least one branch located in Brooklyn.

SQL> select * from branch;

BRANCH_NAME BRANCH_CITY ASSETS

--------------- --------------- ----------

Brighton Brooklyn 7100000

Downtown Brooklyn 9000000

Mianus Horseneck 400000

North Town Rye 3700000

Perryridge Horseneck 1700000

Pownal Bennington 300000

Redwood Palo Alto 2100000

Round Hill Horseneck 8000000

8 rows selected.

SQL> select distinct T.branch_name

2 from branch T,branch S

3 where T.assets>S.assets

4 and S.branch_city='Brooklyn';

BRANCH_NAME

---------------

Round Hill

Downtown

Page 13: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 13

String Operations SQL includes a string-matching operator for comparisons on character strings. The operator “like” uses

patterns that are described using two special characters:

percent (%). The % character matches any substring.

underscore (_). The _ character matches any character.

SQL supports a variety of string operations such as

concatenation (using “||”)

converting from upper to lower case (and vice versa)

Finding string length, extracting substrings, etc.

ASCII

Get The ASCII Value Of

A Character

SELECT ASCII('A') FROM DUAL;

SELECT ASCII('Z') FROM DUAL;

SELECT ASCII('a') FROM DUAL;

SELECT ASCII('z') FROM DUAL;

SELECT ASCII(' ') FROM DUAL;

CASE Related Functions

Upper Case

UPPER(ch VARCHAR2 CHARACTER SET ANY_CS)

RETURN VARCHAR2 CHARACTER SET ch%CHARSET;

SELECT UPPER('Dan Morgan') FROM DUAL;

Lower Case

LOWER(ch VARCHAR2 CHARACTER SET ANY_CS)

RETURN VARCHAR2 CHARACTER SET ch%CHARSET;

SELECT LOWER('Dan Morgan') FROM DUAL;

Initial Letter Upper Case

INITCAP(ch VARCHAR2 CHARACTER SET ANY_CS)

RETURN VARCHAR2 CHARACTER SET ch%CHARSET;

SELECT INITCAP('DAN MORGAN') FROM DUAL;

CHR

Character

SELECT(CHR(68) || CHR(65) || CHR(78)) FROM DUAL;

SELECT(CHR(68) || CHR(97) || CHR(110)) FROM DUAL;

COALESCE

Returns the first non-null

occurrence

COALESCE(<value>, <value>, <value>, ...)

CREATE TABLE test (

col1 VARCHAR2(1),

Page 14: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 14

col2 VARCHAR2(1),

col3 VARCHAR2(1));

INSERT INTO test VALUES (NULL, 'B', 'C');

INSERT INTO test VALUES ('A', NULL, 'C');

INSERT INTO test VALUES (NULL, NULL, 'C');

INSERT INTO test VALUES ('A', 'B', 'C');

SELECT COALESCE(col1, col2, col3) FROM test;

CONCAT

Concatenate

SELECT CONCAT('Dan ', 'Morgan') FROM DUAL;

MAX

The Maximum String

based on the current sort

parameter

MAX(<character_string>)

SELECT MAX(table_name)

FROM user_tables;

MIN

The Minimum String

based on the current sort

parameter

MIN(<character_string>)

SELECT MIN(table_name)

FROM user_tables;

Find the names of all customers whose street address includes substring 'Main'.

SQL> select * from customer;

CUSTOMER_NAME CUSTOMER_STREET

-------------------- ------------------------------

CUSTOMER_CITY

------------------------------

Adams Spring

Pittsfield

Brooks Senator

Brooklyn

Curry North

Rye

Page 15: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 15

CUSTOMER_NAME CUSTOMER_STREET

-------------------- ------------------------------

CUSTOMER_CITY

------------------------------

Glenn Sand Hill

Woodside

Green Walnut

Stamford

Hayes Main

Harrison

CUSTOMER_NAME CUSTOMER_STREET

-------------------- ------------------------------

CUSTOMER_CITY

------------------------------

Johnson Alma

Palo Alto

Jones Main

Harrison

Lindsay Park

Pittsfield

CUSTOMER_NAME CUSTOMER_STREET

-------------------- ------------------------------

CUSTOMER_CITY

------------------------------

Smith North

Rye

Turner Putnam

Stamford

Williams Nassau

Princeton

12 rows selected.

Page 16: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 16

SQL> select customer_name from customer where customer_street like '%Main%';

CUSTOMER_NAME

--------------------

Hayes

Jones

Page 17: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 17

Ordering the display of tuples: SQL offers the user some control over the order in which tuples in a relation are displayed. The order by

clause causes the tuples in the result of a query to appear in sorted order. We may specify desc for

descending order or asc for ascending order, for each attribute; ascending order is the default.

List in alphabetic order (ascending and descending) all customers who have a loan at Perryridge

branch.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

SQL> select distinct customer_name

2 from borrower,loan

3 where borrower.loan_number=loan.loan_number

4 and branch_name='Perryridge'

5 order by customer_name;

CUSTOMER_NAME

--------------------

Adams

Hayes

Page 18: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 18

SQL> select distinct customer_name

2 from borrower,loan

3 where borrower.loan_number=loan.loan_number

4 and branch_name='Perryridge'

5 order by customer_name desc;

CUSTOMER_NAME

--------------------

Hayes

Adams

SQL> select distinct customer_name

2 from borrower,loan

3 where borrower.loan_number=loan.loan_number

4 and branch_name='Perryridge'

5 order by customer_name asc;

CUSTOMER_NAME

--------------------

Adams

Hayes

List the entire loan relation in descending order of amount.If several loans have the same amount then

order them in ascending order by loan number.

SQL> select * from loan;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

L-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

7 rows selected.

SQL> select * from loan

2 order by amount desc,loan_number asc;

LOAN_NUMBE BRANCH_NAME AMOUNT

---------- --------------- ----------

Page 19: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 19

L-23 Redwood 2000

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-11 Round Hill 900

L-93 Mianus 500

7 rows selected.

Page 20: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 20

Set Operations: The SQL operations union, intersect, and except operate on relations and correspond to the relational-

algebra operations ∪ , ∩, and −. Like union, intersection, and set difference in relational algebra, the

relations participating in the operations must be compatible; that is, they must have the same set of

attributes.

In union operation, the number of duplicate tuples in the result is equal to the total number of

duplicates that appear in both d and b. Thus, if Jones has three accounts and two loans at the bank,

then there will be five tuples with the name Jones in the result.

In intersection operation, the number of duplicate tuples that appear in the result is equal to the

minimum number of duplicates in both d and b. Thus, if Jones has three accounts and two loans at

the bank, then there will be two tuples with the name Jones in the result.

In except operation, the number of duplicate copies of a tuple in the result is equal to the number of

duplicate copies of the tuple in d minus the number of duplicate copies of the tuple in b, provided

that the difference is positive. Thus, if Jones has three accounts and one loan at the bank, then there

will be two tuples with the name Jones in the result. If, instead, this customer has two accounts and

three loans at the bank, there will be no tuple with the name Jones in the result.

The set of all customers who have an account at the bank

SQL> select * from depositor;

CUSTOMER_NAME ACCOUNT_NU

-------------------- ----------

Hayes A-102

Johnson A-101

Johnson A-201

Jones A-217

Lindsay A-222

Smith A-215

Turner A-305

7 rows selected.

SQL> select customer_name from depositor;

CUSTOMER_NAME

--------------------

Hayes

Johnson

Johnson

Jones

Lindsay

Smith

Page 21: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 21

Turner

7 rows selected.

The set of customers who have loan at the bank.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hayes L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> select customer_name from borrower;

CUSTOMER_NAME

--------------------

Adams

Curry

Hayes

Jones

Smith

Smith

Williams

7 rows selected.

Find all the bank customer having loan,an account or both at the bank.

SQL> (select customer_name from depositor)

2 union

3 (select customer_name from borrower);

CUSTOMER_NAME

--------------------

Adams

Curry

Hayes

Johnson

Page 22: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 22

Jones

Lindsay

Smith

Turner

Williams

9 rows selected.

SQL> (select customer_name from depositor)

2 union all

3 (select customer_name from borrower);

CUSTOMER_NAME

--------------------

Hayes

Johnson

Johnson

Jones

Lindsay

Smith

Turner

Adams

Curry

Hayes

Jones

CUSTOMER_NAME

--------------------

Smith

Smith

Williams

14 rows selected.

Find all the bank customer having loan and an account at the bank.

SQL> (select customer_name from depositor)

2 intersect

3 (select customer_name from borrower);

CUSTOMER_NAME

--------------------

Hayes

Jones

Page 23: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 23

Smith

SQL> (select distinct customer_name from depositor)

2 intersect

3 (select distinct customer_name from borrower);

CUSTOMER_NAME

--------------------

Hayes

Jones

Smith

SQL> (select distinct customer_name from depositor)

2 intersect all

3 (select distinct customer_name from borrower);

intersect all

*

ERROR at line 2:

ORA-00928: missing SELECT keyword

SQL> (select customer_name from depositor)

2 intersect all

3 (select customer_name from borrower);

intersect all

*

ERROR at line 2:

ORA-00928: missing SELECT keyword

Find all customers who have an account but no loan at the bank.

SQL> (select distinct customer_name from depositor)

2 except

3 (select customer_name from borrower);

except

*

ERROR at line 2:

ORA-00933: SQL command not properly ended

SQL> (select customer_name from depositor)

2 except

3 (select customer_name from borrower);

except

*

Page 24: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 24

ERROR at line 2:

ORA-00933: SQL command not properly ended

SQL> (select customer_name from depositor)

2 except all

3 (select customer_name from borrower);

except all

*

ERROR at line 2:

ORA-00933: SQL command not properly ended

SQL> select * from depositor;

CUSTOMER_NAME ACCOUNT_NU

-------------------- ----------

Hays A-102

Johnson A-101

Johnson A-201

Jones A-217

Lindsay A-222

Smith A-215

Turner A-305

7 rows selected.

SQL> select * from borrower;

CUSTOMER_NAME LOAN_NUMBE

-------------------- ----------

Adams L-16

Curry L-93

Hays L-15

Jones L-17

Smith L-11

Smith L-23

Williams L-17

7 rows selected.

SQL> (select customer_name from depositor) minus (select customer_name from borrower);

CUSTOMER_NAME

--------------------

Johnson

Page 25: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 25

Lindsay

Turner

Page 26: 4. Basic Structure of SQL Queries

Mrs. Sunita M Dol, CSE Dept

SQL Manual Page 26

Practice Problem Statements 1. Find the names of all instructors.

2. Find the department names of all instructors.

3. Find the department names of all instructors, and remove duplicates.

4. List the tuples of instructor relation with salary multiplied by 100.

5. Find the names of all instructors in the Computer Science department who have salary greater than

$70,000.

6. Retrieve the names of all instructors, along with their department names and department building

name.

7. Find instructor names and course identifiers for instructors in the Computer Science department.

8. For all instructors in the university who have taught some course, find their names and the course ID

of all courses they taught.

9. Find the names of all instructors whose salary is greater than at least one instructor in the Biology

department.

10. Find the names of all instructors whose salary is greater than at least one instructor in the Biology

department.

11. List in alphabetic order all instructors in the Physics department.

12. List the entire instructor relation in descending order of salary. If several instructors have the same

salary, then order them in ascending order by name.

13. Find the names of instructors with salary amounts between $90,000 and $100,000.

14. Find the instructor names and the courses they taught for all instructors in the Biology department

who have taught some course.

15. To find the set of all courses taught either in Fall 2009 or in Spring 2010, or both.

16. To find the set of all courses taught in the Fall 2009 as well as in Spring 2010.

17. To find all courses taught in the Fall 2009 semester but not in the Spring 2010 semester.

References:

Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill

International Edition) sixth edition.

Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill

International Edition) fifth edition.

http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/

http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/

http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/