100% found this document useful (1 vote)
113 views62 pages

Structured Query Language

Structured Query Language (SQL) is used to manage relational databases. The document provides an overview of SQL commands like SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER and DROP used to retrieve, manipulate, and define data. It includes examples of creating tables, inserting rows, and using SELECT statements to retrieve data using conditions, functions, joins and aggregate functions. Grouping and filtering rows using clauses like WHERE, HAVING and ORDER BY is also demonstrated through examples on sample Employee and Department tables.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
100% found this document useful (1 vote)
113 views62 pages

Structured Query Language

Structured Query Language (SQL) is used to manage relational databases. The document provides an overview of SQL commands like SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER and DROP used to retrieve, manipulate, and define data. It includes examples of creating tables, inserting rows, and using SELECT statements to retrieve data using conditions, functions, joins and aggregate functions. Grouping and filtering rows using clauses like WHERE, HAVING and ORDER BY is also demonstrated through examples on sample Employee and Department tables.

Uploaded by

Vasantha Kumari
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 62

Structured Query Language

Overview of SQL
 DML
 DDL
 Triggers and integrity constraints
 Embedded and Dynamic SQL
 Security
Basic SQL Commands
Statement Description
SELECT Data retrieval statement.
INSERT Data Manipulation Language
UPDATE (DML).
DELETE Add rows, change data, and delete
few rows.
CREATE Create new tables/views, remove
ALTER tables/ views, and change the
DROP schema.
RENAME
COMMIT Modified values of database are
ROLLBACK permanently written into disk,
SAVEPOINT rollback the changes made.
GRANT Access control can be assigned or
REVOKE changed.
Example tables
Employee
SSN Name BDate Salary MgrSSN DNo
1111 Deepak 5-Jan-62 22000 4444 1
2222 Nandagopal 10-Dec-60 30000 4444 3
3333 Pooja 22-Jan-65 18000 2222 2
4444 Prasad 11-Jan-57 32000 Null 3
5555 Reena 15-Jan-85 8000 4444 3
Department
DNo DName Loc
1 Admin Chennai
2 Research Bangalore
3 Accounts Bangalore
DDL
CREATE TABLE Department(
DNo number(3) not null,
DName varchar2(10) not null,
Loc varchar2(15),
primary key (DNo));
CREATE TABLE Employee(
SSN number(4) not null,
Name varchar2(20) not null,
BDate date,
Salary number(10,2),
MgrSSN number(4),
DNo number(2) not null,
foreign key (DNo) references Department(DNo));
Data Retrieval Statement
(SELECT)
 Syntax
SELECT *|{[DISTINCT] column | expression}
FROM table(s);

The basic SELECT statement must include


the following:
- A SELECT clause.
- A FROM clause.
 Example-1
SELECT * FROM Employee;
The * indicates that it should retrieve all the
columns from Employee table. The output of this
query is shown below:

 Output-1
SSN NAME BDATE SALARY MGRSSN DNO
---- -------------------- --------- --------- --------- ---------
4444 Prasad 11-JAN-57 32000 3
5555 Reena 15-JAN-85 8000 4444 3
1111 Deepak 05-JAN-62 22000 4444 1
2222 Nandagopal 10-DEC-60 30000 4444 3
3333 Pooja 22-JAN-65 18000 2222 2
 Example-2
SELECT * FROM Employee
ORDER BY SSN;
 Output-2
SSN NAME BDATE SALARY MGRSSN DNO
----- -------------------- --------- --------- --------- ---------------------------------
1111 Deepak 05-JAN-62 22000 4444 1
2222 Nandagopal 10-DEC-60 30000 4444 3
3333 Pooja 22-JAN-65 18000 2222 2
4444 Prasad 11-JAN-57 32000 3
5555 Reena 15-JAN-85 8000 4444 3
 Using arithmetic operators
SELECT Name, Salary, Salary * 12
FROM Employee;
 Using aliases
An alias when used for a column:
Renames a column heading
It is useful in arithmetic calculations.
AS keyword can optionally be used between column
name and alias name.
 Example-3
SELECT Name, Salary, Salary * 12 AS YRLY_SALARY
FROM Employee;
OR
SELECT Name, Salary, Salary * 12 "YRLY_SALARY"
FROM Employee;
 Example-4

DESCRIBE Employee;
OR
DESC Employee;

 Output-4
Name Null? Type
------------------------------- -------- ----
SSN NOT NULL NUMBER(4)
NAME NOT NULL VARCHAR2(20)
BDATE DATE
SALARY NUMBER(10,2)
MGRSSN NUMBER(4)
DNO NOT NULL NUMBER(2)
Select Statement with Where
 Example-5
SELECT Name, Salary
FROM Employee
WHERE Salary > 25000;
 Example-6
SELECT DName, Loc
FROM Department
WHERE Loc = 'Bangalore';
 Example-7
SELECT Name, BDate
FROM Employee
WHERE BDate = '11-Jan-57';
 Example-8
SELECT Name, BDate
FROM Employee
WHERE Salary BETWEEN 25000 AND 30000;
 Example-9
SELECT SSN, Name
FROM Employee
WHERE DNo IN (1, 2);
 Example-10
SELECT Name
FROM Employee
WHERE Name LIKE 'P%';
 Example-11
SELECT Name, DNo
FROM Employee
WHERE BDate LIKE '__-JAN-__';
 Example-12
SELECT Name
FROM Employee
WHERE MgrSSN IS NULL;
 Example-13
SELECT Name, Salary, DNo
FROM Employee
WHERE Salary > 30000 AND DNo = 3;
 Example-14
SELECT Name, Salary
FROM Employee
WHERE Name LIKE 'P%' OR Salary <= 20000;
 Example-15
SELECT Name, Salary, DNo
FROM Employee
ORDER BY DNo DESC, Name;
SQL Functions
 MOD(m, n) Returns the remainder of m/n.
 ABS(n) Absolute value of n.
 CEIL(n) Smallest integer larger than n.
 FLOOR(n) Largest integer smaller than n.
 EXP(n) en
 POWER(n, m) nm
 SQRT(n) Square root of n.
 SIGN(n) 1 if n is positive, -1 if negative, 0
if zero.
 VSIZE(n) Storage size of n.
Working with Dates
 Example-16 (MONTHS_BETWEEN)
SELECT MONTHS_BETWEEN(SYSDATE, '09-JAN-1983')
"Experience"
FROM DUAL;
 Output-16
Experience
---------------
247.73471

 Example-17 (GREATEST & LEAST)


The function GREATEST finds the earliest date and LEAST finds the
oldest date in the list.
SELECT GREATEST('10-JAN-93', '10-JAN-98'),
LEAST('10-JAN-93', '10-JAN-98')
FROM DUAL;
 Output-17
GREATEST( LEAST('10
--------- ---------
10-JAN-98 10-JAN-93
Conversion Functions
 VARCHAR2 or CHAR is converted to NUMBER

 VARCHAR2 or CHAR is converted to DATE

 NUMBER is converted to VARCHAR2

 DATE is converted to VARCHAR2


Use of TO_CHAR
 TO_CHAR function converts a date format to a character
string
 Example-18
SELECT Name, Bdate, TO_CHAR(BDate,
'DD/MM/YY') AS "Formatted"
FROM Employee
WHERE SSN = 2222;
 Example-19
SELECT Name, Bdate, TO_CHAR(BDate, 'Month, ddth, YYYY')
AS "Formatted"
FROM Employee
WHERE Name = 'Pooja';
 Output-19
NAME BDATE Formatted
-------------------- --------- ---------------------
Pooja 22-JAN-65 January , 22nd, 1965
Use of TO_DATE
 TO_DATE function is to convert any character literal string into a
valid date format.
 Example-22
 SELECT TO_DATE('1-Sep-2003', 'DD/MM/YYYY')
FROM DUAL;
 Output-22
TO_DATE('
---------
01-SEP-03

 Example-23
SELECT TO_DATE('08/30/2003', 'DD/MM/YYYY')
FROM DUAL;
 Output-23
ERROR at line 1:
ORA-01843: not a valid month
Character Functions
Program Output
SELECT LOWER('Bangalore') bangalore
FROM DUAL;

SELECT UPPER('Bangalore') BANGALORE


FROM DUAL;

SELECT INITCAP('bangalore Bangalore Institute Of


institute of technology') Technology
FROM DUAL;

SELECT CONCAT('Database ', Database Management


'Management')
FROM DUAL;
SELECT SUBSTR('Database', 5, 4) base
FROM DUAL;
SELECT LENGTH('Database') 8
FROM DUAL;

SELECT INSTR('Database', 'b') 5


FROM DUAL;
SELECT INSTR('Database', 'x') 0
FROM DUAL;

SELECT LPAD(Salary, 8, '*') ***22000


FROM Employee
WHERE SSN = 1111;

SELECT RPAD(Salary, 8, '*') 22000***


FROM Employee
WHERE SSN = 1111;
SELECT LTRIM(' Database', ' ') Database
Aggregate Functions

 COUNT
 AVG
 MAX
 MIN
 STDDEV
 SUM
 VARIANCE
 Example-24
SELECT COUNT(*) AS "No. of Employees"
FROM Employee;
 Example-25
SELECT SUM(Salary) AS Total
FROM Employee;
 Example-26
SELECT Name, MAX(Salary), MIN(Salary)
FROM Employee;
GROUP BY Clause
The rules to be followed while using GROUP BY
clause are given below:
 You can't have non-group function or column in
SELECT clause.
 Group functions ignore nulls.
 By default the result of GROUP BY clause sort the
data in ascending order.
 Example:
SELECT DNo, SUM(Salary), COUNT(*), AVG(Salary)
FROM Employee
GROUP BY DNo;
 Example-27
SELECT DNo, SUM(Salary), COUNT(*),
AVG(Salary)
FROM Employee
GROUP BY DNo, MgrSSN;
 HAVING clause
SELECT DNo, AVG(Salary)
FROM Employee
GROUP BY DNo
HAVING DNo = 3;
The order of evaluation when all the clauses are
specified is given below:
1. First all rows matching the WHERE conditions
are retrieved.
2. These rows are grouped using the column(s) in
GROUP BY clause.
3. Finally, groups matching the HAVING clause
condition are retained.
SELECT DNo, AVG(Salary)
FROM Employee
WHERE BDate LIKE '__-JAN-__'
GROUP BY DNo
HAVING MAX(Salary) > 10000;
MULTITABLE QUERIES

 Simple Equi-Joins : guidelines


– Table names in the FROM clause is separated
with commas.
– Use appropriate joining condition. This means
that the foreign key of table1 will be made equal
to the primary key of table2.
– When the attributes or columns have the same
names, tag them with table names using dot
notation.
– Without proper joining condition or attributes the
SQL will display the Cartesian product of the
tables in the FROM clause.
 Example-28: Display the employee names and
the department names for which they work.
SELECT Name, DName
FROM Employee, Department
WHERE Employee.DNo = Department.DNo;

 Example-29 : Display only employees working


for Accounts department.
SELECT Name, Salary, DName
FROM Employee, Department
WHERE (Employee.DNo = Department.DNo)
AND (DName = 'Accounts');
Self-Join and Table Aliases

 Example-30 : Find the employee who


earns more than ‘Nandagopal’.
SELECT e1.Name, e1.Salary
FROM Employee e1, Employee e2
WHERE (e1.Salary > e2.Salary) AND
(e2.Name = 'Nandagopal');
NESTED QUERIES or SUB QUERIES
SELECT <column(s)>
FROM table
WHERE <condn operator>
(SELECT <column>
FROM table);

 Outer query uses the result of the inner query.


 If the inner query returns only one row it is called as
single row sub queries.
 Alternatively if the inner query returns a set of rows
(more than one row) it is classified as multiple-row sub
queries.
 The comparison condition may be a single row operator
like >, =, >=, <, <=, <>) or multiple row operators like
IN, ANY, ALL.
Single-Row Nested Queries
 Display the names of the
employees working for Accounts Employee
department.
SSN Name BDate Salary MgrSSN DNo
SELECT Name
FROM Employee 1111 Deepak 5-Jan-62 22000 4444 1
WHERE DNo = 2222 Nandagopal 10-Dec-60 30000 4444 3
(SELECT DNO 3333 Pooja 22-Jan-65 18000 2222 2
FROM
Department 4444 Prasad 11-Jan-57 32000 Null 3
WHERE DName = 5555 Reena 15-Jan-85 8000 4444 3
'Accounts');
Department
 Display names of employees
whose salary is greater than the DNo DName Loc
employee SSN=1111. 1 Admin Chennai
SELECT Name 2 Research Bangalore
FROM Employee 3 Accounts Bangalore
WHERE Salary >

(SELECT Salary
FROM
Employee
WHERE SSN = 1111);
SSN Name BDate Salary MgrSSN DNo
1111 Deepak 5-Jan-62 22000 4444 1
2222 Nandagopal 10-Dec-60 30000 4444 3
3333 Pooja 22-Jan-65 18000 2222 2
4444 Prasad 11-Jan-57 32000 Null 3
 Example-33 5555 Reena 15-Jan-85 8000 4444 3
 Display all the employees
drawing more than or equal
to the average salary of
DNo DName Loc
department number 3. 1 Admin Chennai
SELECT Name 2 Research Bangalore
FROM Employee 3 Accounts Bangalore

WHERE Salary >=


(SELECT AVG(Salary)
FROM Employee
GROUP BY DNO
HAVING DNo = 3);
Multiple-Row Nested Queries

 IN:Equal to any member in the list.


 ANY: Compare value to each value
returned by the subquery.
 ALL: Compare value to all the values
returned by the subquery.
 Display the name of the highest paid
employee.
SELECT Name, Salary
FROM Employee
SSN Name BDate Salary MgrSSN DNo
WHERE Salary =
(SELECT MAX(Salary) 1111 Deepak 5-Jan-62 22000 4444 1
FROM Employee); 2222 Nandagopal 10-Dec-60 30000 4444 3
3333 Pooja 22-Jan-65 18000 2222 2
SELECT Name, Salary 4444 Prasad 11-Jan-57 32000 Null 3
FROM Employee
WHERE Salary IN
5555 Reena 15-Jan-85 8000 4444 3
(SELECT MAX(Salary)
FROM Employee);

DNo DName Loc


Both '=' and 'IN' works, because the inner
1 Admin Chennai
query produces a single
2 Research Bangalore
tuple.
3 Accounts Bangalore
 Find the Name and Salary of people who
draw in the range Rs. 20,000 to Rs.
40,000. SSN Name BDate Salary MgrSSN DNo
Select Name, Salary from Employee 1111 Deepak 5-Jan-62 22000 4444 1
where Salary = 2222 Nandagopal 10-Dec-60 30000 4444 3
(Select Salary from Employee 3333 Pooja 22-Jan-65 18000 2222 2
where Salary between 20000 and
40000);
4444 Prasad 11-Jan-57 32000 Null 3
5555 Reena 15-Jan-85 8000 4444 3
 Error: ORA-01427: single-row subquery
returns more than one row
 Correct Query:
DNo DName Loc
Select Name, Salary from Employee
1 Admin Chennai
where Salary IN 2 Research Bangalore
(Select Salary from Employee 3 Accounts Bangalore
where Salary between 20000 and
40000);
 Example-34:
SELECT Name, Salary
FROM Employee
WHERE Salary < ANY
(SELECT Salary
FROM Employee
WHERE DNo = 3);
 Example-35:
SELECT Name, Salary
FROM Employee
WHERE Salary > ANY
(SELECT Salary
FROM Employee
WHERE DNo = 3);
 Example-35:
SELECT Name, Salary
FROM Employee
WHERE Salary < ALL SSN Name BDate Salary MgrSSN DNo
(SELECT Salary FROM 1111 Deepak 5-Jan-62 22000 4444 1
Employee WHERE DNo = 3);
2222 Nandagopal 10-Dec-60 30000 4444 3
 Example-36: 3333 Pooja 22-Jan-65 18000 2222 2
SELECT Name, Salary 4444 Prasad 11-Jan-57 32000 Null 3
FROM Employee
WHERE Salary > ALL
5555 Reena 15-Jan-85 8000 4444 3
(SELECT Salary
FROM Employee
WHERE DNo = 3);
 >ALL means greater than the DNo DName Loc
greatest and <ALL means less than 1 Admin Chennai
the lowest value. 2 Research Bangalore
3 Accounts Bangalore
CREATING and ALTERING
DATABASE OBJECTS
 Table: A tabular structure that stores
data.
 View: A tabular structure similar to a
table but it is a collection of one or
more tables.
 Sequence: Automatically generates a
sequence of numbers.
CREATE TABLE Employee(
SSN Number(4) not null,
Name Varchar2(20) not null,
BDate Date,
Salary Number(10,2),
MgrSSN Number(4),
DNo Number(2) not null, Primary Key (SSN),
Foreign Key (MgrSSN) references Employee(SSN),
Foreign Key (DNo) references Department(DNo));
 Names of the tables/views
SELECT *
FROM TAB;
 Schema details of a table
DESC Employee;

Name Null? Type


------------------------------- -------- ----
SSN NOT NULL NUMBER(4)
NAME NOT NULL VARCHAR2(20)
BDATE DATE
SALARY NUMBER(10,2)
MGRSSN NUMBER(4)
DNO NOT NULL NUMBER(2)
 ON DELETE constraint
CREATE TABLE Department(
DNo Number(3) not null,
DName Varchar2(10) not null,
Loc Varchar2(15),
Primary Key (DNo),
Manager Number(4) references
Employee(SSN) ON DELETE CASCADE);
 Creating tables with a Subquery
CREATE TABLE Emp AS
SELECT SSN, Name, Salary
FROM Employee;
ALTER TABLE Statement
ALTER TABLE Employee
ADD Phone Number(7) not null;
(Note: to add not null constraint, the column must be empty)

ALTER TABLE Employee


MODIFY Phone Varchar2(10);
(Note: to modify data type, the column must be empty)

ALTER TABLE Employee


DROP COLUMN Phone;
Disabling and Enabling Constraints
CREATE TABLE Employee(
………………………
………………………
Salary Number(10,2) CONSTRAINT Ch_Sal
CHECK (Salary > 0));

ALTER TABLE Employee


DISABLE CONSTRAINT Ch_Sal;

ALTER TABLE Employee


ENABLE CONSTRAINT Ch_Sal;
CREATE VIEW Statement
The advantages of using views are:
 It restricts data access.
 Reduces joining of more tables often.
 Many users can access a particular view with proper privileges.
 Example
CREATE VIEW Emp_Dept
AS SELECT SSN, Name, DName, Salary
FROM Employee E, Department D
WHERE E.DNo = D.DNo;

View created.
Restrictions on Views

There are few restrictions on views:


 You can not insert new rows nor update the
view table.
(Error Message: ORA-01776: cannot modify more than one
base table through a join view)
 You can not alter the constraints or data
types of the columns.
 If any changes are made to the base
table(s), view table will get updated
automatically.
CREATE SEQUENCE Statement
 How to use a Sequence?
– Step-1:
CREATE SEQUENCE Dept_Seq START WITH 10;
– Step-2:
INSERT INTO Department
values(Dept_Seq.NEXTVAL, 'Sales', 'Belgaum');

 Dropping a sequence
DROP SEQUENCE Dept_Seq;
DML Statements
 INSERT Statement
INSERT INTO Employee
VALUES (1111, 'Deeapk', '5-Jan-62', 22000, 4444, 1);

 Inserting through Keyboard


INSERT INTO Employee
VALUES (&SSN, &Name, &BDate, &Salary, &MgrSSN, &DNo);

 Inserting dates
INSERT INTO Employee
VALUES (6666, 'John', TO_DATE('5-Jan-2003 3:40', 'DD-
MM-YYYY HH24:SS'), 22000, 4444, 1);
 Inserting rows from an existing table
INSERT INTO EMP2
SELECT *
FROM Employee;
 DELETE Statement
DELETE Employee
WHERE SSN = 1111;
 UPDATE Statement
UPDATE Employee
SET DNo = 1
WHERE Name = 'Nandagopal';
 To hike the salary of all employees by 10%.
UPDATE Employee
SET Salary = Salary * 1.05;
Thank You
ADDITIONAL EXAMPLES
 Company Database Example
Employee(SSN , Name , Bdate, Address ,Sex,Salary,
SuperSSN ,DNo);

Department(Dnumber Number(2), DName Varchar2(10),


MgrSSN Char(9), MgrStartDate Date);

Project(PNumber Number(2), PName Varchar(10), Plocation


Varchar2(15), Dnum Number(2));

Dependent(ESSN Char(9), Dependent_Name Varchar2(15),


Sex Char, Bdate Date, Relationship Varchar2(10));

Dept_Locations(DNumber Number(2), Dlocation Varchar2(15));

Works_On(ESSN Char(9), PNo Number(2), Hours Number(3,1));


Example-1: Find all employees who were born during
1980.
Example-2: Calculate the wages earned by each
employee, assuming the remuneration for each hour is
Rs. 250.00.
Example-3: Retrieve the department number, number
of employees in each department and their average
salary.
Example-4: For each project, retrieve the project
number, the project name, and the number of employees
who work on that project.
Example-5: For each project on which more than 3
employees work, retrieve the project number, the project
name, and the number of employees who work on that
project.
Example-6: Print the number of employees whose
salaries exceed more than Rs. 25,000/- in each
department. Display the department name also.
 Example-4: For each project, retrieve the
project number, the project name, and the
number of employees who work on that project.

 Example-5: For each project on which more


than 3 employees work, retrieve the project
number, the project name, and the number of
employees who work on that project.

 Example-6: Print the number of employees


whose salaries exceed more than Rs. 25,000/-
in each department. Display the department
name also.
 Example-1: Find all employees who were born during 1980.
SELECT Name
FROM EMPLOYEE
WHERE BDate LIKE '__-___-80';

 Example-2: Calculate the wages earned by each employee,


assuming the remuneration for each hour is Rs. 250.00.
SELECT Essn, Sum(Hours),Sum(Hours) * 250
FROM Works_On
GROUP BY Essn;

 Example-3: Retrieve the department number, number of


employees in each department and their average salary.
SELECT DNo, Count(*), Avg(Salary)
FROM Employee, Department
WHERE DNo = DNumber
GROUP BY DNo;
 Example-4: For each project, retrieve the project number, the project
name, and the number of employees who work on that project.
SELECT PNumber, PName, Count(*)
FROM Project, Works_On
WHERE PNumber = PNo
GROUP BY PNumber, PName;

 Example-5: For each project on which more than 3 employees work,


retrieve the project number, the project name, and the number of
employees who work on that project.
SELECT PNumber, PName, Count(*)
FROM Project, Works_On
WHERE PNumber = PNo
GROUP BY PNumber, PName
HAVING Count(*) > 3;

 Example-6: Print the number of employees whose salaries exceed more


than Rs. 25,000/- in each department. Display the department name also.
SELECT DName, Count(*)
FROM Department, Employee
WHERE DNumber = DNo AND Salary > 25000
GROUP BY DName;
STUDENT Database Example
Student(Student_id char(10), SName
varchar2(10), Major varchar2(5), GPA
number(3,1))
Faculty(Faculty_id char(4), FName char(4),
Dept varchar2(5), Desig varchar2(5), Salary
number(10,2))
Course(Course_id char(5), CName
varchar2(10), Faculty_id char(4))
Enrol(Course_id char(5), Student_id char(10),
Grade char(1))
 Example-1: List the names of all students enrolled for the course
'IS6T1'.
SELECT SName
FROM Student
WHERE Student_id In(
SELECT Student_id
FROM Enrol
WHERE Course_id = 'IS6T1');
 Example-2: List the names of students enrolled for the course 'IS6T1'
and have received 'A' grade.
SELECT SName
FROM Student
WHERE Student_id In(
SELECT Student_id
FROM Enrol
WHERE Course_id = 'IS6T1' AND Grade = 'A');
 Example-3: List all the departments having an average salary of
above Rs. 10000.
SELECT Dept
FROM Faculty
GROUP BY Dept
HAVING AVG(Salary) > 10000;
 Example-5: List the names of all faculty
members beginning with 'P' and ending
with letter 'A'.

SELECT FName
FROM Faculty
WHERE SUBSTR(FName,1,1) LIKE 'P'
AND SUBSTR(FName,-2,1) LIKE 'A';
More Examples
 Book Dealer Database
AUTHOR (Authorid : Int, Name : String, City : String,
Country : String)
PUBLISHER (Publisherid : Int, Name : String, City : String,
Country : String)
CATALOG (Bookid : Int, Title : String, Authorid : Int,
Publisherid : Int, Categoryid : Int, ear : Int, Price : Int)
CATEGORY (Categorid : Int, Description : String)
ORDER_DETAILS (OrderNo : Int, Bookid : Int, Quantity : Int)
Queries
 Give the details of the authors who have 2 or
more books in the catalog and the price of the
books is greater than the average price of the
books in the catalog and the year of publication is
after 2000.
 Query
select C.Authorid, A.AName
from Catalog C, Author A
where A.Authorid = C.Authorid and C.Year > 2000 and
C.Price >
(Select Avg(Price) from Catalog)
group by C.Authorid, A.AName
having count(C.Authorid) >= 2;
 Find the number of the book which has
maximum sales.

Create View SalesDetails as (


Select OD.Bookid as Book#, C.Price as Cost,
Sum(OD.Quantity) as Qty, Sum(OD.Quantity *
C.Price) as Sales
from Order_Details OD, Catalog C, Author A
where OD.Bookid = C.Bookid and C.Authorid =
A.Authorid
group by OD.Bookid, C.Price);
select A.Authorid, A.AName,S.book#, S.Sales
from Author A, catalog C, Salesdetails S
where A.Authorid = C.Authorid and S.Book# = C.Bookid
and sales =
(select Max(Sales) from salesdetails);
 Student Enrollment Database
STUDENT (RegNo : String, Name : String, Major
: String, BDate : date)
COURSE (Course# : Int, CName : String, Dept :
String)
ENROLL (RegNo : String, Course# : Int, Sem :
Int, Marks : Int)
BOOK_ADOPTION (Course# : Int, Sem : Int,
ISBN : Int)
TEXT (ISBN : Int, BookTitle : String, Publisher :
String, Author : String)
 Produce a list of text books (include Course#,
ISBN, BookTitle) in the alphabetical order for
courses offered by the 'CS' department that
use more than two books
Select C.Course#, T.ISBN, T.BookTitle
from Course C,Book_Adoption BA, Text T
where C.Course# = BA.Course# and BA.ISBN =
T.ISBN and C.Dept = 'CSE'
group by C.Course#, T.ISBN, T.BookTitle
order by T.BookTitle;
End of Chapter 5

You might also like