0% found this document useful (0 votes)
15 views

Lab 5

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab 5

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DATABASE LAB MANUAL

LAB-5

Objectives
- Understand COUNT() function in SQL.
- Understand (ORDER BY) keyword in SQL.
- Understand (AND & OR) operators in SQL.
- Understand (NOT) operator in SQL.
- Understand (IS NULL & IS NOT NULL) operators in SQL.

COUNT Function:
This function will count the items in the selected column,

Select Count(column1) from table_name


or
Select Count(*) from table_name

ORDER BY:

The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
SELECT * FROM table_name
ORDER BY column1 ASC, column2 DESC;

AND & OR Operators:

The AND operator is used to filter records based on more than one condition.
The WHERE clause can contain one or many AND operators.

The AND operator displays a record if all the conditions are TRUE.
The OR operator displays a record if any of the conditions are TRUE.

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT column1, column2, ...


FROM table_name
WHERE condition1 OR condition2 OR condition3 ...
NOT Operator:
The NOT operator is used in combination with other operators to give the opposite
result, also called the negative result.

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;

IS NULL & IS NOT NULL Operators:


To test for null values, we cannot use comparison operators(=,<,>), we have to use
the IS NULL & IS NOT NULL operators.

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
LAB WORK

Q1: Count the number of students who have a scholarship.


Q2: List all students ordered by their age in descending order.
Q3: Find students who are either in the 'Computer Science' major or have a
grade 'A' and are not receiving a scholarship.
Q4: List all students who do not have an email address.
Q5: Find all students who have provided their email address.

Here are the SQL commands to create and fill the table:

CREATE TABLE Students (


student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade CHAR(1),
major VARCHAR(50),
email VARCHAR(50),
scholarship BOOLEAN
);

INSERT INTO Students (student_id, name, age, grade, major, email, scholarship) VALUES
(1, 'Alice', 20, 'A', 'Computer Science', '[email protected]', TRUE),
(2, 'Bob', 22, 'B', 'Engineering', NULL, FALSE),
(3, 'Charlie', 19, 'A', 'Mathematics', '[email protected]', TRUE),
(4, 'David', 21, 'C', 'Physics', '[email protected]', FALSE),
(5, 'Eva', 20, 'B', 'Biology', NULL, TRUE),
(6, 'Frank', 23, 'D', 'Chemistry', '[email protected]', FALSE),
(7, 'Grace', 21, 'A', 'Computer Science', '[email protected]', TRUE),
(8, 'Hannah', 19, 'B', 'Engineering', '[email protected]', TRUE),
(9, 'Isaac', 22, 'C', 'Mathematics', NULL, FALSE),
(10, 'Judy', 20, 'A', 'Physics', '[email protected]', TRUE);

You might also like