Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SIXTH LAB Aims Implementation

(* All codes are compiled on Oracle Live SQL server*)


Submitted By:-
Name-Vikalp Kumar Tripathi
Branch- DSAI
Roll no.-201020258
Create Table Authors
Code

CREATE TABLE Authors


(Author_Id INT NOT NULL,
First_Name Varchar(20) NOT NULL,
Last_Name varchar(20) NOT NULL);
Output

Insert Into Authors


Code

INSERT INTO authors(Author_Id,First_Name,Last_Name)


VALUES
(1 ,"Jack" ,"London"),
(2, "Robert", "Heinlein"),
(3,"Elleston","Trevor"),
(4,"David","Feintuch");

Output
Create Table Books
Code

CREATE TABLE Books(


Book_Id int NOT NULL,
Title Varchar(50) NOT NULL,
PAGES INT NOT NULL,
PUBLICATION_DATE date NOT NULL,
Author_Id int NOT NULL);

Output

Insert Into Books


Code

INSERT INTO books(Book_Id,Title,PAGES,PUBLICATION_DATE,Author_Id)


VALUES
(1,"Midshipman’s Hope",483,'2010/01/01',4),
(2,"Challenger’s Hope",588,'2001/10/01',4),
(3,"The Flight of the Phoenix",739,"1992/04/05",3),
(4,"Starship Troopers",256,"1991/12/01",2),
(5,"Red Planet",113,'2025/03/16',2),
(6,"Farmer in the Sky",127,"1990/12/08",1);

Output
WRITE THE SQL FOR:-

1. Write a query to display the book name only of books containing less than 150 pages

Code

SELECT Title AS Book_Name FROM books WHERE PAGES<150;

Output

2. Write a query to display the book name only of all books with an AuthorID of 4

Code

SELECT Title AS Book_Name FROM books WHERE Author_Id=4;


Output

3. Write a query to display all books with a publication date before 2/1/2010
Code

SELECT Title AS Book_Name FROM books WHERE PUBLICATION_DATE <'2010/02/01';

Output

4. Write a query that will display the book name, author first name, author last name and
number of pages. Do not display any authors that do not have a book
Code

SELECT books.Title AS Book_Name, authors.First_Name,authors.Last_Name, books.PAGES


FROM books
INNER JOIN authors ON books.Author_Id=authors.Author_Id;
Output

5. Write a query showing the book name and author first and last name for all books with a page
count greater than 150 pages

Code

SELECT books.Title AS Book_Name, authors.First_Name,authors.Last_Name


FROM books
INNER JOIN authors ON books.Author_Id=authors.Author_Id
WHERE books.PAGES>150;

Output

6. Create a view that will display the book name, author first name, author last name and
number of pages in the book.

Code

CREATE VIEW Book_Details AS


SELECT books.Title AS Book_Name, authors.First_Name,authors.Last_Name,books.PAGES
FROM books , authors
WHERE books.Author_Id=authors.Author_Id;
SELECT * FROM Book_Details;
Output

7. Write a SQL query that will query all data using the view defined in query6 but only display
books with a page count greater than 200 and less than 500.
Code

SELECT Book_Name,First_Name,Last_Name,PAGES
FROM book_details
WHERE PAGES BETWEEN 200 AND 500;
Output

8. Write a query to display all book titles and publication dates sorted by publication date oldest
to most recent.

Code

SELECT Title, PUBLICATION_DATE FROM books


ORDER BY (PUBLICATION_DATE);

Output

You might also like