Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Page |1

INFORMATION TECHNOLOGY AND INFORMATION SYSTEM


DEPARTMENT

DATABASE MANAGEMENT SYSTEM

EXERCISE

7
DML QUERIES

Kiel Martin P. Benzon IT 201


NAME SECTION
12/6/2023 12/6/2023
DATE PERFORMED DATE FINISHED

GJPRosales
Page |2

I. OBJECTIVES

At the end of this exercise, students must be able to:

a. To insert, update and delete contents on the table


b. Create select statements to query data
c. To organize the data in the table using order by
d. Use aggregate and scalar functions in filtering the data

II. BACKGROUND INFORMATION

• A DML statement is executed when you:


 Add new rows to a table
 Modify existing rows in a table
 Remove existing rows from a table
• A transaction consists of a collection of DML statements that form a logical
unit of work.

INSERT
• Add new rows to a table by using the INSERT statement:

INSERT INTO table [(column [, column...])]


VALUES (value [, value...]);

• With this syntax, only one row is inserted at a time.

• Adds one or more rows to a table


• Inserting into a table

• Inserting a record that has some null attributes requires identifying


the fields that actually get data

• Inserting from another table

GJPRosales
Page |3

UPDATE
• Modify existing values in a table with the UPDATE statement:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

• Update more than one row at a time (if required).

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:

SELECT column, group_function


FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];

SELECT STATEMENT WITH AGGREGATE FUNCTIONS


Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value

GJPRosales
Page |4

SUM() - Returns the sum

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

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

SQL ORDER BY Syntax

SELECT column_name, column_name


FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Example:
SELECT * FROM Customers
ORDER BY Country ASC;

SELECT * FROM Customers


ORDER BY Country;

The SQL BETWEEN Operator


The BETWEEN operator selects values within a range. The values can be
numbers, text, or dates.

SQL BETWEEN Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

GJPRosales
Page |5

Example:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;

BETWEEN Operator with IN Example

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.

SQL LIKE Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

Tip: The "%" sign is used to define wildcards (missing letters) both
before and after the pattern.

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.

GJPRosales
Page |6

 It is a data definition language (DDL) statement rather than a DML


statement; cannot easily be undone.

TRUNCATE TABLE table_name;

A database transaction consists of one of the following:


 DML Statements that constitute one consistent change to the data.
 One DDL statement.
 One data control language (DCL) statement.

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

III. EXPERIMENTAL PROCEDURE

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.

PROVIDE A SCREENSHOT OF THE SCRIPT AND THE OUTPUT PER STEP

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.

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.

5. Display the last name and department ID of all employees in departments 20 or 50


in ascending alphabetical order by name.

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.

CREATE TABLE my_employee


(id numeric(4) CONSTRAINT my_employee_id_nn NOT NULL,
last_name VARCHAR(25),
first_name VARCHAR(25),
userid VARCHAR(8),
salary numeric(9,2));

GJPRosales
P a g e | 10

2. Describe the structure of the MY_EMPLOYEE table to identify the column


names.

3. Execute the beginning of the transaction

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

ID LAST_NAME FIRST_NAME USERID SALARY

GJPRosales
P a g e | 11

1 Patel Ralph rpatel 895

2 Dancs Betty bdancs 860

3 Biri Ben bbiri 1100

4 Newman Chad cnewman 750

5 Ropeburn Audrey aropebur 1550

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.

6. Confirm your addition to the table.

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

8. Confirm your additions to the table.

GJPRosales
P a g e | 13

9. Make the data additions permanent.

A. Update and delete data in the MY_EMPLOYEE table.

10. Create the beginning of the transaction here.

11. Change the last name of employee 3 to Drexler.

12. Change the salary to $1,000 for all employees who have a salary less than $900.

GJPRosales
P a g e | 14

13. Verify your changes to the table.

14. Delete Betty Dancs from the MY_EMPLOYEE table.

15. Confirm your changes to the table.

GJPRosales
P a g e | 15

16. Commit all pending changes.

IV. QUESTION AND ANSWER

1. Distinguish between Truncate and Delete Command

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

You might also like