ADBMS LAB Exercise 2 - Ubas
ADBMS LAB Exercise 2 - Ubas
EXERCISE
2
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
3. The HR department needs to find high-salary and low-salary employees. Modify Task
1 to display the last name and salary for any employee whose salary is not in the range
of $5,000 to $12,000.
GJPRosales
Page |9
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.
GJPRosales
P a g e | 10
GJPRosales
P a g e | 11
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
P a g e | 12
7. Display all employee last names in which the third letter of the name is “a.”
GJPRosales
P a g e | 13
8. Display the last names of all employees who have both an “a” and an “e” in their last
name.
GJPRosales
P a g e | 14
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 | 15
2. Describe the structure of the MY_EMPLOYEE table to identify the column names.
GJPRosales
P a g e | 16
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 | 17
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 | 18
GJPRosales
P a g e | 19
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 | 20
GJPRosales
P a g e | 21
GJPRosales
P a g e | 22
GJPRosales
P a g e | 23
12. Change the salary to $1,000 for all employees who have a salary less than $900.
GJPRosales
P a g e | 24
GJPRosales
P a g e | 25
GJPRosales
P a g e | 26
GJPRosales
P a g e | 27
GJPRosales
P a g e | 28
The difference between the Truncate and Delete Command is that Truncate, when used,
won’t be able to rollback, it reset the identity of the table, it locks the entire table, it is from
the DDL (Data Definition Language) command, and won’t be able to use WHERE clause
with it, whilst with the Delete is we are able to use the rollback after using it, it does not
reset the identity of the table, it locks the table row, it is from DML(Data Manipulation
Language) command and we can use the WHERE filter data to delete. Another point is
that when using delete, it is possible to delete one or more rows all at once but with the
Truncate command, each row is removed from the table simultaneously.
GJPRosales
P a g e | 29
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