0% found this document useful (0 votes)
9 views12 pages

Exp 7

This document outlines an experiment in a Computer Engineering course focused on SQL nested queries and pattern matching. It includes objectives, theoretical explanations, SQL examples, and tasks for students to perform, such as creating tables and writing specific queries. The document also specifies deliverables and concludes with a summary requirement for students.

Uploaded by

saishj40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Exp 7

This document outlines an experiment in a Computer Engineering course focused on SQL nested queries and pattern matching. It includes objectives, theoretical explanations, SQL examples, and tasks for students to perform, such as creating tables and writing specific queries. The document also specifies deliverables and concludes with a summary requirement for students.

Uploaded by

saishj40
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

DEPARTMENT OF COMPUTER ENGINEERING

[NBA Accredited]

EXPERIMENT 7

To demonstrate concept of Nested Queries.


Objective :
To write SQL queries to demonstrate pattern matching.

In nested queries, a query is written inside a query. The result of inner query is used in execution
Theory: of outer query.

A query is usually added within the WHERE Clause of another SELECT query.

The comparison operators, including >, <, or = can be used. The comparison operator can also be
a operator which is used in more than one row, such as IN, ANY, SOME, or ALL.

A nested query can be treated as an inner query, which is a SQL query placed as a part of another
query called as outer query.

The inner query is executed first and then the outer query so that the results of the inner query can
be used by the outer query.

Example:
SELECT employee_id,first_name,last_name,salary

FROM employees WHERE salary >

(SELECT AVG(SALARY) FROM employees);

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

The percent sign (%) represents zero, one, or multiple characters


The underscore sign (_) represents one, single character.

A wildcard character is used to substitute one or more characters in a string.

Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE
clause to search for a specified pattern in a column.

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]

Using the [charlist] Wildcard

The following SQL statement selects all customers with a City starting with "b", "s", or
"p"

SELECT * FROM Customers

WHERE City LIKE '[bsp]%';

The following SQL statement selects all customers with a City starting with "a", "b", or "c":

SELECT * FROM Customers

WHERE City LIKE '[a-c]%';

The two following SQL statements select all customers with a City NOT starting with "b", "s", or
"p":

SELECT * FROM Customers

WHERE City LIKE '[!bsp]%';

OR can be also written as

SELECT * FROM Customers


WHERE City NOT LIKE '[bsp]%';

Performance Create following tables and insert appropriate data.

MOVIE(movieid, Title, Genre, length, releaseyear, budget, hitstatus, productionname)

DIRECTOR(did, dname, gender)

ACTOR(aid, aname, gender)

ACTS(aid, movieid, role, fees)

DIRECTS(did, movieid, numdays)

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]
Table Movie

Table Actor

Table Director

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]
Table Acts

Directs

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]
Write SQL queries to retrieve following information.

1. Find name of the director/s for all the movies with HIT status.

2. List all names of all the movies directed by female directors.

3. Find name/s of the actors with highest fees charged,

4. List maximum and average budget and number of movies for the movies for each
Production.

5. List all the movies releases in the 1990’s decade ie all the movies with year release 199* ,
where * could be nay number from 1 to 9.

6. List all the movies with title that includes word ‘Khiladi’.

7. List all the movies with genre that includes Action.

8. Find the name/s of the director who took more than average days to direct a movie.

Deliverables: 1. SQL queries for each question in performance section


2. Screen shots of the results

Conclusion Students should summarize the understandings in their own words

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]

create database demo;

use demo;

create table Movie(

Movie_id int NOT NULL,

Title varchar(100),

Genre varchar(100),

Year int,

Budget int,

Hit_Status varchar(100),

Production_Name varchar(100),

CONSTRAINT PK_Movie PRIMARY KEY(Movie_id)

);

describe movie;

INSERT INTO Movie values(1,'Cars','Adventure',2006, 1200000,'BLOCK BUSTER','Pixar');

INSERT INTO Movie values(2,'Deadpool','Adventure',2016, 5800000,'HIT','20th Century Fox');

INSERT INTO Movie values(3,'Avatar','Sci-fi',2009, 2370000,'BLOCK BUSTER','20th Century Fox');

INSERT INTO Movie values(4,'Titanic','Romance',1997, 200000,'BLOCK BUSTER','20th Century Fox');

INSERT INTO Movie values(5,'6 Underground','Action',2019, 15000000,'AVERAGE','Skydance Bay Films');

INSERT INTO Movie values(6,'The Maze Runner','Sci-fi',2014, 340000,'FLOP','20th Century Fox');

INSERT INTO Movie values(7,'Inception','Sci-fi',2010, 16000000,'Superhit','Warner Bros');

INSERT INTO Movie values(8,'The Conjuring','Horror',2013,2000000,'Hit','Warner Bros');

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]
INSERT INTO Movie values(9,'Annabelle','Horror',2014, 600000, 'FLOP','Warner Bros');

INSERT INTO Movie values(10,'Jack Reacher','Action',2012, 600000,'Hit','Paramount Pictures');

INSERT INTO Movie values(11,'Top Gun:Maverick','Action',2022, 170,'BLOCK BUSTER','Paramount


Pictures');

INSERT INTO Movie values(12,'The Amazing Spider-Man','Action',2012, 2000000,'BLOCK BUSTER','Sony


Pictures');

INSERT INTO Movie values(13,'Iron Man','Sci-fi',2008, 1400000,'Superhit','Marvel Studios');

INSERT INTO Movie values(14,'Avengers:Endgame','Sci-fi',2019, 35600000,'BLOCK BUSTER','Marvel


Studios');

INSERT INTO Movie values(15,'Intersteller','Sci-fi',2014, 16500000,'BLOCK BUSTER','Paramount Pictures');

SELECT * FROM MOVIE;

create table Director(

Did int NOT NULL,

Dname varchar(100) ,

Gender CHAR,

CONSTRAINT PK_Dir PRIMARY KEY(Did)

);

INSERT INTO Director VALUES(100,'John Lasseter','M');

INSERT INTO Director VALUES(101,'James Cameron','M');

INSERT INTO Director VALUES(102,'Christoper Nolan','M');

INSERT INTO Director VALUES(103,'James Wan','M');

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]

INSERT INTO Director VALUES(104,'Martha Coolidge','F');

INSERT INTO Director VALUES(105,'Kathryn Bigelow','F');

select * from director

create table Actor(

A_id int NOT NULL,

A_name varchar(100),

Gender char,

CONSTRAINT PK_Actor PRIMARY KEY(A_id)

);

describe actor;

INSERT INTO Actor VALUES(1001,'Robert Downey Jr.','M');

INSERT INTO Actor VALUES(1002,'Andrew Garfield.','M');

INSERT INTO Actor VALUES(1003,'Leonardo DiCaprio','M');

INSERT INTO Actor VALUES(1004,'Gal Gadot','F');

INSERT INTO Actor VALUES(1005,'Anne Hathaway','F');

INSERT INTO Actor VALUES(1006,'Emma Stone','F');

select * from actor;

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]

Create Table Acts(Movie_id int NOT NULL,

A_id int NOT NULL,

Role varchar(100),

Fees int,

PRIMARY KEY(Movie_id,A_id),

FOREIGN KEY (Movie_id) REFERENCES Movie(Movie_id),

FOREIGN KEY (A_id) REFERENCES Actor(A_id)

);

desc acts;

drop table acts;

INSERT INTO Acts VALUES(2,1005,'Wade Wilson', 200000);

INSERT INTO Acts VALUES(4,1003,'Jack Dawson', 2500000);

INSERT INTO Acts VALUES(5,1005,'One', 2080000);

INSERT INTO Acts VALUES(7,1003,'Cobb', 120000);

INSERT INTO Acts VALUES(10,1004,'Jack Reacher', 30000);

INSERT INTO Acts VALUES(11,1004,'Pete Michell', 2500000);

INSERT INTO Acts VALUES(12,1002,'Peter Parker',20000);

INSERT INTO Acts VALUES(4,1004,'Kelly', 2000000);

INSERT INTO Acts VALUES(11,1002,'Ron', 150000);

INSERT INTO Acts VALUES(5,1003,'Harry', 800000);

update acts

set fees = 2500000

where role = 'Pete Michell' ;

select * from acts;

INSERT INTO Acts VALUES(12,1006,'Gwen Stacy', 260000);

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]
INSERT INTO Acts VALUES(13,1001,'Tony Stark',570000);

INSERT INTO Acts VALUES(14,1001,'Tony Stark', 670000);

create Table Directs (D_id int NOT NULL,

Movie_id int NOT NULL,

No_of_Days int,

PRIMARY KEY(D_id,Movie_id),

FOREIGN KEY (D_id) REFERENCES Director(Did),

FOREIGN KEY (Movie_id) REFERENCES Movie(Movie_id)

);

INSERT INTO Directs VALUES(100,1,90);

INSERT INTO Directs VALUES(101,3,1460);

INSERT INTO Directs VALUES(101,4,160);

INSERT INTO Directs VALUES(102,7,2000);

INSERT INTO Directs VALUES(102,15,100);

INSERT INTO Directs VALUES(103,9,80);

INSERT INTO Directs VALUES(103,8,38);

INSERT INTO Directs VALUES(104,12,150);

INSERT INTO Directs VALUES(105,5,30);

INSERT INTO Directs VALUES(102,10,60);

INSERT INTO Directs VALUES(105,2,60);

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]

select * from movie;

select * from Actor;

select * from director;

select * from acts;

select * from directs;

/* 1.Find name of the director/s for all the movies with HIT status*/

select dc.Dname , m.title

from directs as d join movie as m on d.movie_id =m.movie_id join director as dc on dc.did = d. d_id

where m. hit_status = 'Hit';

/* 2. List all names of all the movies directed by female directors */

select dc.Dname , m.title

from directs as d join movie as m on d.movie_id =m.movie_id join director as dc on dc.did = d. d_id

where dc.gender = 'f';

/* 3. Find name/s of the actors with highest fees charged */

select a_name, fees, title

from actor a join acts ac on a. a_id = ac.a_id join movie m on m. movie_id = ac. movie_id

where fees = (

select max(fees)

from acts );

/* 4. List maximum and average budget and number of movies for the movies for each Production.*/

select AVG(budget), Max(budget), count(movie_id), Production_Name

from movie

Prof Kadambari Deherkar Department of Computer Engineering


DEPARTMENT OF COMPUTER ENGINEERING
[NBA Accredited]
group by Production_Name;

/* 5. List all the movies releases in the 2000’s decade ie all the movies with year release 200* , where * could be
nay number from 1 to 9. */

Select title

from movie

where year like '200_';

/* 6. List all the movies with title that includes word ‘khiladi’ */

select title

from movie

where title like '%khiladi%';

/*7. List all the movies with genre that includes drama*/

select title

from movie

where genre like '%action%';

/* 8. Find the name/s of the director who took more than average days to direct a movie. */

select d.Dname, d.Gender, m.title, dr.no_of_days

from director d join directs dr on d.did = dr.d_id join movie m on m. movie_id = dr.movie_id

where dr.no_of_days >= (

select avg(no_of_days)

from directs; );

Prof Kadambari Deherkar Department of Computer Engineering

You might also like