0% found this document useful (0 votes)
16 views7 pages

68 Cee 4 B 58 Afd 5 Lab 2 Database Systems

Bookshelf in roman urdu to the store ? to the house s2 to be a little more whiter shade of it but give me its algorithm

Uploaded by

gfss83505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

68 Cee 4 B 58 Afd 5 Lab 2 Database Systems

Bookshelf in roman urdu to the store ? to the house s2 to be a little more whiter shade of it but give me its algorithm

Uploaded by

gfss83505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Dawood University of Engineering and

Technology Karachi-74800
Department of Computer Science
Database Systems

LAB-02 Operators, Filtering, and Sorting

OBJECTIVE(S)
 Learn Operators
 Learn how to filter data
 Learn how to sort data

QUERIES
 SELECT

In SQL, the SELECT statement is used to select data from database tables. Often, you want
to select a subset of rows or columns or both. The result of the SELECT statement is called a
result set.

 SELECT * FROM tb_name;


 SELECT col1_name, col2_name FROM tb_name;

 SELECT DISTINCT

In SQL, you can use the DISTINCT clause with the SELECT statement to select only
unique values or removes duplicates.

 SELECT DISTINCT col_name FROM tb_name;


 SELECT DISTINCT col1_name, col2_name FROM tb_name;

 WHERE

The WHERE clause in SELECT statements can be used to filter the records. We can also
use the WHERE clause with UPDATE, DELETE, etc.

 SELECT col_name FROM tb_name WHERE condition;


Dawood University of Engineering and
Technology Karachi-74800
Department of Computer Science
Database Systems

TASK-1
 Alter the student table created in Lab 01 to have the following
columns: ID, Name, DOB, Semester, Department, GPA
 Insert/update the records with variations in DOB, semester, department, and GPA.
 Display all the records.
 Display only the ID, name, and department of all the students.
 Display a list of all the departments (without any repetitions).
 Select distinct department from student;
 Display the name as “Student Name”, department as “Department”, and DOB
as “Date of Birth”.

OPERATORS
An operator is a character or reserved word in SQL. Many operators are primarily used with
the WHERE clause to perform various operations, such as logical and comparison
operations.

 Arithmetic operators
 Comparison operators
 Logical operators

Arithmetic Operators

Operator Description
+ (Addition) Adds values on either side of the operator.
- (Subtraction) Subtracts the right-hand operand from the left-hand operand.
* (Multiplication) Multiplies values on either side of the operator.
/ (Division) Divides left-hand operand by right-hand operand.
Divides left-hand operand by right-hand operand and returns remainder.
% (Modulus)

Comparison Operators

Operator Description
= Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal
Instructor: Engr. Poonam K.K 2
Dawood University of Engineering and
Technology Karachi-74800
Department of Computer Science
Database Systems

<= Less than or equal

Instructor: Engr. Poonam K.K 3


Dawood University of Engineering and
Technology Karachi-74800
Department of Computer Science
Database Systems

Logical Operators

Operator Description
The AND operator allows the existence of multiple conditions in an SQL
AND statement's WHERE clause. The AND operator displays a result if all conditions
in the WHERE clause are TRUE.
The OR operator is used to combine multiple conditions in an SQL statement's
OR WHERE clause. The OR operator displays a result if any one of the conditions
in the WHERE clause is TRUE.
The NOT operator reverses the meaning of the logical operator with which it is
NOT
used. The NOT operator displays a result if a condition is NOT TRUE.
The IN operator is used to compare a value to a list of literal values that have been
IN specified. The IN operator allows us to specify multiple values in the WHERE
clause.
The BETWEEN operator is used to search for values that are within a set of
BETWEEN values, given the minimum value and the maximum value. The values can be
numbers, text, and dates.
The LIKE operator is used to SELECT data based on patterns. It is used to
compare a value to similar values using wildcard operators. MySQL allows the use
of two wildcards:
LIKE
 Percentage (%) wildcard allows us to match any string of zero or more
characters.
 The Underscore (_) wildcard allows us to match any single character.

AND/OR/NOT/IN

 SELECT col_name(s) FROM tb_name


WHERE ((condition1) AND (condition2)) ;

SELECT STUDENT FROM STUDENT


WHERE (STUDENT_ID=1) AND (STUDENT_NAME = “ABC”);

 SELECT col_name(s) FROM tb_name


WHERE condition1 OR condition2;

 SELECT col_name(s) FROM tb_name


WHERE NOT condition1;

 SELECT col_name(s) FROM tb_name

Instructor: Engr. Poonam K.K 4


Dawood University of Engineering and
Technology Karachi-74800
Department of Computer Science
Database Systems

WHERE col_name IN (value1, value2);

Select employee_ID from employees


Where employee_ID = 9 IN (1,5,8,3);

BETWEEN
 SELECT col_name(s) FROM tb_name
WHERE col_name BETWEEN value1 AND value2;

 SELECT col_name(s) FROM tb_name


WHERE col_name NOT BETWEEN value1 AND value2;

For string/text values:


 SELECT col_name(s) FROM tb_name
WHERE col_name NOT BETWEEN “value1” AND “value2”;
LIKE

 SELECT col_name(s) FROM tb_name


WHERE col_name LIKE pattern;
Select* from student
Where st_name like “%n”;

St_Id St_Name St_Age


1 Ahmed 21
2 naila 22
3 Nain 23
4 Shayan 25

LIKE “a%” Values start with ‘a’


LIKE “%a” Values end with ‘a’
LIKE “%a Values having ‘a’ in any position
%”
LIKE “_a%” Values having ‘a’ in the second position
LIKE “a%z” Values starts with ‘a’ ends with ‘z’

Instructor: Engr. Poonam K.K 5


Dawood University of Engineering and
Technology Karachi-74800
Department of Computer Science
Database Systems

TASK-2
 Display all the information of all the students belonging to any one
department.
 Display only the name, semester, and GPA of all the students belonging to any
one particular department.
 Display the name, semester, and department of all the students who are not in
a particular department.
 Display the name, semester, GPA, and department of all the students whose
GPA is less than 2.5 AND who are in the 5th semester or above.
 Display the name and semester of students who belong to a particular
department or whose GPA is greater than 3.5.
 Display the name, DOB, and department of students born after 2000 or
whose semesters are not yet in the third semester.
 Display the names and GPAs of students whose GPA lies between 2.5 and 3.5.
 Display a list of all the students with an “a” in their name.

ORDER BY
The ORDER BY clause in the SELECT statement is used to sort the result set in ascending or
descending order. The ORDER BY clause sorts the records in ascending order by default.

 SELECT col_names(s) FROM tb_name


ORDER BY col_name(s) ASC/DESC;

TASK
 Display the names (in alphabetical order) and departments of all the students.
 Display the name, department, and GPA of all the students ranked from highest
to lowest GPA.
 Display name, department, and GPA of the three students having the highest GPA.
 Display the names and GPAs of the 4th, 5th and 6th ranked students.
 Display the names and GPAs of the students having the lowest three GPAs.

Instructor: Engr. Poonam K.K 6


Dawood University of Engineering and
Technology Karachi-74800
Department of Computer Science
Database Systems

LAB TASK
1. Implement Task1 and Task2 by using Lab-01.

SUBMISSION GUIDELINES

1. Implement the tasks in the MySQL Workbench tool/ Command line


2. Place all the tasks in a folder labeled with Roll No and Lab No. e.g. ‘cs181xxx_Lab01’.
3. Submit the zip file at google classroom
4. 100% policies for plagiarism

Instructor: Engr. Poonam K.K 7

You might also like