Professional Documents
Culture Documents
Data Manipulation Language
Data Manipulation Language
EXERCISE
7
DML QUERIES
GJPRosales
Page |2
I. OBJECTIVES
INSERT
• Add new rows to a table by using the INSERT statement:
GJPRosales
Page |3
UPDATE
• Modify existing values in a table with the UPDATE statement:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
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:
GJPRosales
Page |4
Syntax:
SELECT AVG(column_name) FROM table_name;
SELECT min(column_name) FROM table_name;
SELECT max(column_name) FROM table_name where [condition];
SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed
Example:
SELECT * FROM Customers
ORDER BY Country ASC;
GJPRosales
Page |5
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);
The SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
Tip: The "%" sign is used to define wildcards (missing letters) both
before and after the pattern.
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.
GJPRosales
Page |6
TC commands are:
BEGIN TRAN - begin of transaction
COMMIT TRAN - commit for completed transaction
ROLLBACK - go back to beginning if something was not right in transaction.
GJPRosales
Page |7
PART 1:
Scenario
The HR department needs your assistance in creating some queries.
Task
Write the appropriate SQL statement for the following queries. The result of the
queries will be checked from your computer.
1. Because of budget issues, the HR department needs a report that displays the last
name and salary of employees who earn more than $12,000.
2. Create a report that displays the last name and department number for employee
number 176.
GJPRosales
Page |8
4. Create a report to display the last name, job ID, and hire date for employees with the
last names of Matos and Taylor. Order the query in ascending order by the hire date.
6. Modify Task 3 to display the last name and salary of employees who earn between
$5,000 and $12,000, and are in department 20 or 50. Label the columns Employee and
Monthly Salary, respectively.
GJPRosales
Page |9
7. Display all employee last names in which the third letter of the name is “a.”
8. Display the last names of all employees who have both an “a” and an “e” in their last
name.
PART 2:
Insert data into the MY_EMPLOYEE table.
1. Run the script to build the MY_EMPLOYEE table used in this exercise.
GJPRosales
P a g e | 10
4. Create an INSERT statement to add the first row of data to the MY_EMPLOYEE
table from the following sample data. Do not list the columns in the INSERT
clause. Do not enter all rows yet. USING IMPLICIT INSERTION
GJPRosales
P a g e | 11
5. Populate the MY_EMPLOYEE table with the second row of the sample data from
the preceding list. This time, list the columns explicitly in the INSERT clause.
GJPRosales
P a g e | 12
7. Populate the table with the next two rows of the sample data listed in step 3 by
running the INSERT statement in the script that you created. USING EXPLICIT
INSERTION
GJPRosales
P a g e | 13
12. Change the salary to $1,000 for all employees who have a salary less than $900.
GJPRosales
P a g e | 14
GJPRosales
P a g e | 15
GJPRosales
P a g e | 16
V. REFERENCES
Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2016). Modern Database Management
12th Edition, Prentice Hall.
Microsoft. (2012). Database Administration Fundamentals . USA: Wiley.
GJPRosales