Professional Documents
Culture Documents
CCS0021L
(INFORMATION MANAGEMENT)
EXERCISE
7
DATA MANIPULATION LANGUAGE
•
• Inserting a record that has some null attributes requires identifying the fields that actually get data
SELECT
• Used for queries on single or multiple tables
• Clauses of the SELECT statement:
– SELECT
• List the columns (and expressions) to be returned from the query
– FROM
• Indicate the table(s) or view(s) from which data will be obtained
– WHERE
• Indicate the conditions under which a row will be included in the result
– GROUP BY
• Indicate categorization of results
– HAVING
• Indicate the conditions under which a category (group) will be included
– ORDER BY
• Sorts the result according to specified criteria
Syntax:
Syntax:
SELECT AVG(column_name) FROM table_name;
SELECT min(column_name) FROM table_name;
SELECT max(column_name) FROM table_name where [condition];
Example:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3
should not be displayed:
Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
Tip: The "%" sign is used to define wildcards (missing letters) both before and after the pattern. You will learn more about
wildcards in the next chapter.
The following SQL statement selects all customers with a City ending with the letter "s":
Example:
SELECT * FROM Customers
WHERE City LIKE '%s';
DELETE
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = ‘HI’;
Delete all rows
DELETE FROM CUSTOMER_T;
TRUNCATE Statement
Removes all rows from a table, leaving the table empty and the table structure intact.
It is a data definition language (DDL) statement rather than a DML statement; cannot easily be undone.
SAVEPOINT
Create a marker in the current transaction by using the SAVEPOINT statement.
ROLLBACK
Discard all pending changes by using the ROLLBACK statement.
Roll back to the marker by using the ROLLBACK TO SAVEPOINT statement.
Instructions: Download the two files before starting this exercise, namely: hrcreate.txt and hrpopulate.txt. Both files
are found in Canvas. Copy the contents of hrcreate.txt first to the worksheet in SQL developer and click run script.
Then do the same with hrpopulate.txt file. One you see the tables and contents inside the table, you can start
answering the following:
Write the DML code the show the following. Then for each number, copy and paste the code including the
screenshot of the output.
Queries:
1. Show the employee id, first name, and last names of employees who are managed by manager id
114.
2. Show the employee id and the salary of the employees who has a salary higher than 11,999.
4. Show all the columns of the departments table that has no manager id.
5. Show the country id and country names of countries that are either Italy, Singapore, United States
of America or countries found in region 1.
7. Show all the columns of the employees that has a job id of AD_PRES.
8. Show the job id and the number of employees that has a job id of SA_REP or ST_CLERK. Group
it by job id and arrange it by its job id.
10. Show the salary, and the count of employees who has the same salary. Arrange it by salary in
ascending order. Rename the count of employees to ‘Number_of_Persons’.
12. Add a new employee using the new job id that was created in number 11. The following details are
below:
Employee id is 500
13. Change the last name Delos Santos and salary to 12,500 of the employee with an employee id of
500.
16. Restore the deleted record (employee id 500) back to the employees table.
Rollback SPEmp500;
17. Create a view that has all the details of the employees table.
VIII. REFERENCES
Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2007). Modern Database Management 8th
Edition. New Jersey: Pearson Prentice Hall.