Database Code
Database Code
FROM table_name
WHERE condition
Retrieve the name of all unique films released in the 21st century and onwards, along with
their release years.
Retrieve the names of all the directors and their distinct films shot at City Hall.
Retrieve the number of distributors distinctly who distributed films acted by Clint Eastwood
as 1st actor.
Exercise 1: COUNT
SELECT COUNT(*) FROM FilmLocations;
Retrieve the number of locations of the films which are directed by Woody Allen.
Retrieve the number of rows having a release year older than 1950 from the
“FilmLocations” table.
Retrieve the name of all unique films released in the 21st century and onwards, along with
their release years.
Retrieve the names of all the directors and their distinct films shot at City Hall.
Retrieve the number of distributors distinctly who distributed films acted by Clint Eastwood
as 1st actor.
Exercise 3: LIMIT
SELECT * FROM FilmLocations LIMIT 25;
Retrieve the next 3 film names distinctly after first 5 films released in 2015.
Insert a new instructor record with id 4 for Sandip Saha who lives in Edmonton, CA into the
“Instructor” table.
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.
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 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.
VALUES(8, 'Ryan', 'Steve', 'Barlby', 'GB'), (9, 'Sannareddy', 'Ramesh', 'Hyderabad', 'IN');
UPDATE Instructor
SET city='Toronto'
WHERE firstname="Sandip";
Update the city and country for Doe with id 5 to Dubai and AE respectively.
UPDATE Instructor
WHERE ins_id=5;
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
WHERE ins_id=4;
WHERE ins_id = 6;
FROM EMPLOYEES
SELECT *
FROM EMPLOYEES
Exercise 2: Sorting
Retrieve a list of employees ordered by department ID.
FROM EMPLOYEES
ORDER BY DEP_ID;
FROM EMPLOYEES
Exercise 3: Grouping
For each department ID retrieve the number of employees in the department..
FROM EMPLOYEES
GROUP BY DEP_ID;
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.
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.
FROM EMPLOYEES
GROUP BY DEP_ID
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 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';
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';