0% found this document useful (0 votes)
140 views11 pages

SQL Commands For Book Details: EXP - NO:2 Date

The document describes using SQL commands to create tables for book, author, and publisher data and insert data into those tables. It then provides examples of SQL queries to retrieve data from the tables, including finding book titles by year, price range, author name and country. It also shows aggregate functions to find totals, averages, maximums, and minimums of book prices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views11 pages

SQL Commands For Book Details: EXP - NO:2 Date

The document describes using SQL commands to create tables for book, author, and publisher data and insert data into those tables. It then provides examples of SQL queries to retrieve data from the tables, including finding book titles by year, price range, author name and country. It also shows aggregate functions to find totals, averages, maximums, and minimums of book prices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EXP.

NO:2

DATE: SQL COMMANDS FOR BOOK DETAILS

AIM:

To implement SQL commands for book details.

CREATING THE RELATION:

SQL> create table s_publisher(

p_name varchar(15),

p_address varchar(25),

primary key(p_name));

SQL>create table s_author(

a_name varchar(10),

country varchar(25),

primary key(a_name));

SQL>create table s_book(

isbn_no number(5),

title varchar(10),

pub_yr number(5),

price number(5),

a_name varchar(10),

p_name varchar(15),

primary key(isbn_no),

foreign key (a_name) references s_author(a_name),

foreign key (p_name) references s_publisher(p_name));


INSERTING VALUES INTO RELATIONS:

SQL>insert all into s_author (a_name,country) values('Arora','US') into s_author


(a_name,country) values('Kapoor','Canada') into s_author (a_name,country) values('Basu','India')
into s_author (a_name,country) values('Sinha','India') SELECT * FROM dual;

4 row(s) inserted.

SQL>insert all into s_publisher (p_name,p_address) values('PHI','Delhi') into s_publisher


(p_name,p_address) values('Technical','Pune main bazar')into s_publisher (p_name,p_address)
values('Nirali','Mumbai')into s_publisher (p_name,p_address) values('Scitech','Chennai')
SELECT * FROM dual;

4 row(s) inserted.

SQL>insert into s_book(isbn_no,title,pub_yr,price,a_name,p_name) values


(1001,'Orale',2004,399,'Arora','PHI');

insert into s_book(isbn_no,title,pub_yr,price,a_name,p_name) values


(1002,'DBMS',2004,400,'Basu','Technical') ;

insert into s_book(isbn_no,title,pub_yr,price,a_name,p_name) values


(2001,'DOS',2003,250,'Sinha','Nirali');

insert into s_book(isbn_no,title,pub_yr,price,a_name,p_name) values


(2002,'ADBMS',2004,450,'Basu','Technical');

insert into s_book(isbn_no,title,pub_yr,price,a_name,p_name) values


(2003,'Unix',2000,300,'Kapoor','Scitech');
DISPLAYING THE RELATIONS:

SQL> select * from s_book;

ISBN_NO TITLE PUB_YR PRICE A_NAME P_NAME


2001 DOS 2003 250 Sinha Nirali
2002 ADBMS 2004 450 Basu Technical
2003 Unix 2000 300 Kapoor Scitech
1001 Orale 2004 399 Arora PHI
1002 DBMS 2004 400 Basu Technical
5 rows returned

SQL>select * from s_publisher;

P_NAME P_ADDRESS
PHI Delhi
Technical Pune main bazar
Nirali Mumbai
Scitech Chennai
4 rows returned

SQL>select * from s_author;

A_NAME COUNTRY
Arora US
Kapoor Canada
Basu India
Sinha India
4 rows returned 
1) a.Find all the publisher name.

SQL>select p_name from s_book;

P_NAME
Nirali
Technical
Scitech
PHI
Technical
5 rows returned

1)b.Find unique publisher name.

SQL> select distinct(p_name) from s_book;

P_NAME
Nirali
Technical
PHI
Scitech
4 rows returned
2)Find all the details of the book.

SQL> select * from s_book;

ISBN_NO TITLE PUB_YR PRICE A_NAME P_NAME


2001 DOS 2003 250 Sinha Nirali
2002 ADBMS 2004 450 Basu Technical
2003 Unix 2000 300 Kapoor Scitech
1001 Orale 2004 399 Arora PHI
1002 DBMS 2004 400 Basu Technical
5 rows
returned
3)Find the title of the book published in the year 2004.

SQL> select title from s_book where pub_yr=2004;

TITLE
ADBMS
Orale
DBMS
3 rows returned

4.) Find the title of the book having price b/w 300&400.

SQL> select title from s_book where price between 300 and 400;

TITLE
Unix
Orale
DBMS
3 rows returned
5.) Find the title of the book with the author name and country published in the year 2004.

SQL> select title,s_author.a_name,s_author.country from s_book,s_author where


s_book.a_name=s_author.a_name and s_book.pub_yr=2004;

TITLE A_NAME COUNTRY


ADBMS Basu India
Orale Arora US
BOOK_NAME
DBMS Basu India
DOS
3 rows
returned ADBMS
Unix

6)Display the name of the book as Orale book_name.


DBMS
SQL> select title as book_name 5 rows returned from s_book;

7) Find the title and its price.

SQL> select title ,price from s_book;


TITLE PRICE
DOS 250
ADBMS 450
Unix 300
Orale 399
DBMS 400
TITLE P_ADDRESS COUNTRY
5 rows returned
DOS Mumbai India
ADBMS Pune main bazar India
Unix Chennai Canada
Orale Delhi US
DBMS Pune main bazar India
5 rows
returned

8) Find the title of the book ,its address and country.

SQL> select s_book.title,p_address,s_author.country from s_book,s_publisher,s_author


where s_book.p_name=s_publisher.p_name and s_book.a_name=s_author.a_name;

9) Find the title of the book with author name and country using tuple variable.

SQL> select b.title,a.a_name,a.country from s_book b,s_author a where b.a_name=a.a_name;

TITLE A_NAME COUNTRY


DOS Sinha India
ADBMS Basu India
Unix Kapoor Canada
Orale Arora US
DBMSA_NAME
TITLE Basu India
DOS 5Sinha
rows
returned
ADBMS Basu
Unix Kapoor
10) Find the name of the book and its author name should
Orale Arora
start with ‘Ka’.
DBMS Basu
SQL> select title,a_name from s_book where a_name
5 rows returned
like 'Ka%';

TITLE A_NAME
Unix Kapoor
1 rows returned

11)Select the name of the book and author anme may have a character ‘r’ or ’a’.

SQL> select title,a_name from s_book where a_name like '%r%' or a_name like '%a%';

12) Display the name of all publishers where address have the substring ‘main’.

SQL> select p_name from s_publisher where p_address like '%main%';


P_NAME
Technical
1 rows returned
MAX(PRICE)
13) 450
1 rows returned
i)Find the total price of all the books.

SQL>select sum(price) from s_book;

SUM(PRICE)
1799
1 rows returned  MIN(PRICE)
250
1 rows returned

ii)Find the maximum price of the book table.

SQL> select max(price) from s_book;

iii)Find the minimum price among all the books.

SQL> select min(price) from s_book;


iv)Find the average price of the books.

SQL> select avg(price) from s_book;

AVG(PRICE)
359.8
1 rows returned

14) Find the no. of records from author table.

SQL> select count(*) from s_author;

COUNT(*)
4
1 rows returned

15)Update the country name ‘US’ into ‘America’.

SQL> update s_author set country='America' where country='US';

1 row updated.

SQL> A_NAME COUNTRY select * from s_author;


Arora America
Kapoor Canada
Basu India
Sinha India
4 rows returned 
RESULT:

Thus the implementation of SQL queries for book details was executed
successfully and the output is verified .

You might also like