0% found this document useful (0 votes)
24 views

Database Code

The document discusses SQL queries using various functions like aggregate, scalar, string and date functions. It includes examples of using functions like COUNT, SUM, AVG, ROUND, LENGTH, UCASE, DATE_ADD, DATEDIFF on sample tables.

Uploaded by

Sohail Doulah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Database Code

The document discusses SQL queries using various functions like aggregate, scalar, string and date functions. It includes examples of using functions like COUNT, SUM, AVG, ROUND, LENGTH, UCASE, DATE_ADD, DATEDIFF on sample tables.

Uploaded by

Sohail Doulah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SELECT column1, column2, ...

FROM table_name

WHERE condition

SELECT * FROM FilmLocations;

SELECT Title, Director, Writer FROM FilmLocations;

Retrieve the name of all unique films released in the 21st century and onwards, along with
their release years.

SELECT Title, ReleaseYear, Locations FROM FilmLocations WHERE ReleaseYear>=2001;

Retrieve the names of all the directors and their distinct films shot at City Hall.

SELECT DISTINCT Title, Director FROM FilmLocations WHERE Locations="City Hall";

Retrieve the number of distributors distinctly who distributed films acted by Clint Eastwood
as 1st actor.

SELECT COUNT(DISTINCT Distributor) FROM FilmLocations WHERE Actor1="Clint Eastwood";

Exercise 1: COUNT
SELECT COUNT(*) FROM FilmLocations;

SELECT COUNT(Locations) FROM FilmLocations WHERE Writer="James Cameron";

Retrieve the number of locations of the films which are directed by Woody Allen.

SELECT COUNT(Locations) FROM FilmLocations WHERE Director="Woody Allen";

Retrieve the number of films shot at Russian Hill.

SELECT Count(Title) FROM FilmLocations WHERE Locations="Russian Hill";

Retrieve the number of rows having a release year older than 1950 from the
“FilmLocations” table.

SELECT Count(*) FROM FilmLocations WHERE ReleaseYear<1950;


Exercise 2: DISTINCT
SELECT DISTINCT Title FROM FilmLocations;

SELECT COUNT(DISTINCT ReleaseYear) FROM FilmLocations WHERE ProductionCompany="Warner Bros.


Pictures";

Retrieve the name of all unique films released in the 21st century and onwards, along with
their release years.

SELECT DISTINCT Title, ReleaseYear FROM FilmLocations WHERE ReleaseYear>=2001;

Retrieve the names of all the directors and their distinct films shot at City Hall.

SELECT DISTINCT Title, Director FROM FilmLocations WHERE Locations="City Hall";

Retrieve the number of distributors distinctly who distributed films acted by Clint Eastwood
as 1st actor.

SELECT COUNT(DISTINCT Distributor) FROM FilmLocations WHERE Actor1="Clint Eastwood";

Exercise 3: LIMIT
SELECT * FROM FilmLocations LIMIT 25;

SELECT * FROM FilmLocations LIMIT 15 OFFSET 10;

Retrieve the name of first 50 films distinctly.

SELECT DISTINCT Title FROM FilmLocations LIMIT 50;

Retrieve first 10 film names distinctly released in 2015.

SELECT DISTINCT Title FROM FilmLocations WHERE ReleaseYear=2015 LIMIT 10;

Retrieve the next 3 film names distinctly after first 5 films released in 2015.

SELECT DISTINCT Title FROM FilmLocations WHERE ReleaseYear=2015 LIMIT 3 OFFSET 5;


Exercise 1: INSERT
SELECT * FROM Instructor;

Insert a new instructor record with id 4 for Sandip Saha who lives in Edmonton, CA into the
“Instructor” table.

INSERT INTO Instructor(ins_id, lastname, firstname, city, country)

VALUES(4, 'Saha', 'Sandip', 'Edmonton', 'CA');

nsert two new instructor records into the “Instructor” table. First record with id 5 for John
Doe who lives in Sydney, AU. Second record with id 6 for Jane Doe who lives in Dhaka, BD.

INSERT INTO Instructor(ins_id, lastname, firstname, city, country)

VALUES(5, 'Doe', 'John', 'Sydney', 'AU'), (6, 'Doe', 'Jane', 'Dhaka', 'BD');

Insert a new instructor record with id 7 for Antonio Cangiano who lives in Vancouver, CA into
the “Instructor” table.

INSERT INTO Instructor(ins_id, lastname, firstname, city, country)

VALUES(7, 'Cangiano', 'Antonio', 'Vancouver', 'CA');

SELECT * FROM Instructor;

Insert two new instructor records into the “Instructor” table. First record with id 8 for Steve
Ryan who lives in Barlby, GB. Second record with id 9 for Ramesh Sannareddy who lives in
Hyderabad, IN.

INSERT INTO Instructor(ins_id, lastname, firstname, city, country)

VALUES(8, 'Ryan', 'Steve', 'Barlby', 'GB'), (9, 'Sannareddy', 'Ramesh', 'Hyderabad', 'IN');

SELECT * FROM Instructor;


Exercise 2: UPDATE
Update the city for Sandip to Toronto.

UPDATE Instructor

SET city='Toronto'

WHERE firstname="Sandip";

SELECT * FROM Instructor;

Update the city and country for Doe with id 5 to Dubai and AE respectively.

UPDATE Instructor

SET city='Dubai', country='AE'

WHERE ins_id=5;

SELECT * FROM Instructor;

Update the city of the instructor record to Markham whose id is 1.

UPDATE Instructor

SET city='Markham'

WHERE ins_id=1;

Update the city and country for Sandip with id 4 to Dhaka and BD respectively.

UPDATE Instructor

SET city='Dhaka', country='BD'

WHERE ins_id=4;

SELECT * FROM Instructor;


Exercise 3: DELETE

Remove the instructor record of Doe whose id is 6.

DELETE FROM instructor

WHERE ins_id = 6;

SELECT * FROM Instructor;

Remove the instructor record of Hima.

DELETE FROM instructor

WHERE firstname = 'Hima';

SELECT * FROM Instructor;


Exercise 1: String Patterns
Retrieve all employees whose address is in Elgin,IL.

SELECT F_NAME , L_NAME

FROM EMPLOYEES

WHERE ADDRESS LIKE '%Elgin,IL%';

Retrieve all employees who were born during the 1970's.

Retrieve all employees in department 5 whose salary is between 60000 and


70000.

SELECT *

FROM EMPLOYEES

WHERE (SALARY BETWEEN 60000 AND 70000) AND DEP_ID = 5;

Exercise 2: Sorting
Retrieve a list of employees ordered by department ID.

SELECT F_NAME, L_NAME, DEP_ID

FROM EMPLOYEES

ORDER BY DEP_ID;

Retrieve a list of employees ordered in descending order by department ID


and within each department ordered alphabetically in descending order by
last name.

SELECT F_NAME, L_NAME, DEP_ID

FROM EMPLOYEES

ORDER BY DEP_ID DESC, L_NAME DESC;


In SQL problem 2 (Exercise 2 Problem 2), use department name instead of
department ID. Retrieve a list of employees ordered by department name,
and within each department ordered alphabetically in descending order by
last name.

SELECT D.DEP_NAME , E.F_NAME, E.L_NAME

FROM EMPLOYEES as E, DEPARTMENTS as D

WHERE E.DEP_ID = D.DEPT_ID_DEP

ORDER BY D.DEP_NAME, E.L_NAME DESC;

Exercise 3: Grouping
For each department ID retrieve the number of employees in the department..

SELECT DEP_ID, COUNT(*)

FROM EMPLOYEES

GROUP BY DEP_ID;

For each department retrieve the number of employees in the department,


and the average employee salary in the department..

SELECT DEP_ID, COUNT(*), AVG(SALARY)

FROM EMPLOYEES

GROUP BY DEP_ID;

Label the computed columns in the result set of SQL problem 2 (Exercise 3
Problem 2) as NUMEMPLOYEES and AVGSALARY.

SELECT DEP_ID, COUNT(*) AS "NUM_EMPLOYEES", AVG(SALARY) AS "AVG_SALARY"

FROM EMPLOYEES

GROUP BY DEP_ID;

In SQL problem 3 (Exercise 3 Problem 3), order the result set by Average
Salary..
SELECT DEP_ID, COUNT(*) AS "NUM_EMPLOYEES", AVG(SALARY) AS "AVG_SALARY"

FROM EMPLOYEES

GROUP BY DEP_ID

ORDER BY AVG_SALARY;

In SQL problem 4 (Exercise 3 Problem 4), limit the result to departments with
fewer than 4 employees.

SELECT DEP_ID, COUNT(*) AS "NUM_EMPLOYEES", AVG(SALARY) AS "AVG_SALARY"

FROM EMPLOYEES

GROUP BY DEP_ID

HAVING count(*) < 4

ORDER BY AVG_SALARY;

S
Exercise 2 Solutions: Aggregate Functions
**Query A1:**Â Enter a function that calculates the total cost of all
animal rescues in the PETRESCUE table.
select SUM(COST) from PETRESCUE;

**Query A2:**Â Enter a function that displays the total cost of all
animal rescues in the PETRESCUE table in a column called
SUMOFCOST.
select SUM(COST) AS SUM_OF_COST from PETRESCUE;

**Query A3:**Â Enter a function that displays the maximum quantity of


animals rescued.
select MAX(QUANTITY) from PETRESCUE;

Query A4: Enter a function that displays the average cost of animals
rescued.
select AVG(COST) from PETRESCUE;

Query A5: Enter a function that displays the average cost of rescuing
a dog. Hint - Bear in my the cost of rescuing one dog on day, is
different from another day. So you will have to use and average of
averages.
select AVG(COST/QUANTITY) from PETRESCUE where ANIMAL = 'Dog';

Exercise 3 Solutions: Scalar and String


Functions
**Query B1:**Â Enter a function that displays the rounded cost of
each rescue.
select ROUND(COST) from PETRESCUE;

Query B2: Enter a function that displays the length of each animal
name.
select LENGTH(ANIMAL) from PETRESCUE;

Query B3: Enter a function that displays the animal name in each
rescue in uppercase.
select UCASE(ANIMAL) from PETRESCUE;

Query B4: Enter a function that displays the animal name in each
rescue in uppercase without duplications.
select DISTINCT(UCASE(ANIMAL)) from PETRESCUE;

Query B5: Enter a query that displays all the columns from the
PETRESCUE table, where the animal(s) rescued are cats. Use cat in
lower case in the query.
select * from PETRESCUE where LCASE(ANIMAL) = 'cat';

Exercise 4 Solutions: Date and Time


Functions
Query C1: Enter a function that displays the day of the month when
cats have been rescued.
select DAY(RESCUEDATE) from PETRESCUE where ANIMAL = 'Cat';
Query C2: Enter a function that displays the number of rescues on the
5th month.
select SUM(QUANTITY) from PETRESCUE where MONTH(RESCUEDATE)='05';
Query C3: Enter a function that displays the number of rescues on the
14th day of the month.
select SUM(QUANTITY) from PETRESCUE where DAY(RESCUEDATE)='14';
Query C4: Animals rescued should see the vet within three days of
arrivals. Enter a function that displays the third day from each rescue.
select DATE_add(RESCUEDATE, INTERVAL 3 DAY) from PETRESCUE;
Query C5: Enter a function that displays the length of time the animals
have been rescued; the difference between today’s date and the
recue date.
select DATEDIFF(CURRENT_TIMESTAMP,RESCUEDATE) from PETRESCUE;

You might also like