Lab 8 Data Manipulation Language (Select Where, Order, Distinct, Limit)
Lab 8 Data Manipulation Language (Select Where, Order, Distinct, Limit)
Contents
1. Introduction 3
4. Concept Map 3
4.1. Reading the data from database using SELECT statement. 4
4.2. Specifying some criteria with WHERE clause 4
4.3. Comparison Operators 4
4.4. String Pattern Matching - LIKE and NOT LIKE 5
4.5. IS NULL, IS NOT NULL 6
4.6. Logical Operators - AND, OR: 6
4.7. BETWEEN, NOT BETWEEN 6
5.1 SELECT Advanced 7
5.2 Sorting the ResultSet with ORDER BY Clause 7
5.3 Limiting the Result Set using LIMIT Clause 7
5.4 Listing distinct elements of a column using DISTINCT keyword 8
7. Practice Tasks 13
7.1. Practice Task 1 [Expected time = 50mins] 13
7.2. Outcomes 15
9. Evaluation criteria 15
11. REFERENCES: 16
• https://www.w3schools.com/sql/sql_select.asp
4. Concept Map
In this section, a brief overview of the concepts is presented, those will be used in this lab
Department of Computer Science, Page 3
C.U.S.T.
Lab 8: Data Manipulation Language (SELECT WHERE, ORDER, DISTINCT, LIMIT)
afterwards.
For example, if we want to displays all columns and all rows of the DEPARTMENTS table we
have just write the query like given below:
For Example,
If we want to select all employeeswhose salary is greater than 10,000 from the table
employeesthe query for the statement is written as follows:
4.3.Comparison Operators
For numbers (INT, DECIMAL, FLOAT), you can use comparison operatorsto compare two
numbers, Table 2 enlists some basic comparison operators.
Operator Name
= equal to
<> or != not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
Table 2: Comparison Operator
The wildcard '_'matches any single character; '%' matches any number of characters (including
zero). See Table 3For examples of some patterns.
Pattern Matches to
'abc%' strings beginning with 'abc'
'%xyz' strings ending with 'xyz'
'%aaa%' strings containing 'aaa'
'___' strings containing exactly three characters
'a_b%' strings beginning with 'a', followed by any
single character, followed by 'b', followed by
zero or more characters.
Table 3: Pattern examples for LIKE and NOT LIKE operators
For examples:
To select all employees whose first namebegins with 'a':
For examples:
To select all employees whose first name end with 'a':
For example:
To select all departments whose manager_idis NULL.
For examples:
To select all employees whose salary is greater than 10,000 and first name starts with ‘a%’ the
query for the statement is written as follows:
SELECT * FROM employees WHERE salary > 10000 AND first_name LIKE 'N%';
For examples:
To select all employees whose salary is greater than 5000 or the first name end with ‘%a’ the
query for the statement is written as follows:
SELECT * FROM employees WHERE salary >5000 OR first_name LIKE 'a %';
For example:
To select all employees whose salary is between 500 and 20,000, the query for the statement is
written as follows:
The selected row will be ordered according to the values in columnA, in either ascending (ASC)
(default) or descending (DESC) order. If several rows have the same value in columnA, it will be
ordered according to columnB, and so on.
For Example:
To select all thecountriesfrom the countries table and sort the result set by region id in ascending
order, the following statement can be written:
Offset is the index of record in the result set, indexes starts from zero (0).
For Example:
To shows first 10 employees from employee table, the query of a statement can be written as:
For Example:
To show first_name and last_name of employee, 3nd to 4threcords of the result set of employees
table, an offset can be defined along with required number of rows, the following statement can
be written as:
If you want to retrieve a unique salaries from the employee table so we can write the query for
the statements is given below:
5.1.Task 1
Read Data Manipulation Language (DML).
6. Procedure& Tools
In this section, procedure of the tasks and setup of required tools is defined.
To select all employeesfirst_name and last_name whose salary is greater than 10,000 and first
name starts with ‘a%’ the query for the statement is written as follows:
SELECT first_name, last_name FROM employees WHERE quantity > 5000 AND name LIKE 'a
%';
To select all employeesfirst_name and last_name whose salary is between 10,000 and 20,000,
the query for the statement is written as follows:
SELECT first_name,last_name FROM employees WHERE salary between 10,000 and 20,000;
Showing unique salary from employees table we have write the query like given below:
Figure 9: Distinct
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\fs\assignments$
1. Createaquerytodisplaythelastnameandsalaryofemployeesearningmorethan12,000 Rupees.
2. Createaquerytodisplaytheemployeelastnameanddepartmentnumberforemployeenumber
176.
3. Displaytheemployeelastname,jobID,andstartdateofemployeeshiredbetweenFebruary20, 1998, and
May 1, 1998. Order the query in ascending order by startdate
4. Displaythelastnameanddepartmentnumberofallemployeesindepartments20and50in
alphabetical order byname.
5. Show the list of last name and salary of employees who earn between 5,000 and 12,000Rupees, and
are in department 20 or 50. Label the columns Employeeand Monthly Salary,
respectively
6. Display the last name and hire date of every employee who was hired in1994.
7. Display the last name and job title of all employees who do not have amanager.
8. Displaythelastname,salary,andcommissionforallemployeeswhoearncommissions.Sort
data in descending order of salary andcommissions.
9. Displaythelastnamesofallemployeeswherethethirdletterofthenameisana
Department of Computer Science, Page 14
C.U.S.T.
Lab 8: Data Manipulation Language (SELECT WHERE, ORDER, DISTINCT, LIMIT)
10. Displaythelastnameofallemployeeswhohaveanaandaneintheirlastname.
11. Displaythelastname,job,andsalaryforallemployeeswhosejobissalesrepresentativeorstock clerk and
whose salary is not equal to 2,500, 3,500, or7,000 Rupees.
12. Displaythelastname,salary,andcommissionforallemployeeswhose commission amount is 20%
7.2.Outcomes
After completing this lab, student will be able to understand the use of DML statements and
importing and exporting a database.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10.1. Books
Text Book:
10.2. Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\fs\lectures$\
11. REFERENCES: