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

St. Xavier'S College: (Affiliated To Tribhuvan University) Maitighar, Kathmandu

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

ST.

XAVIER’S COLLEGE
(Affiliated to Tribhuvan University)
Maitighar, Kathmandu

DBMS LAB ASSIGNMENT #03

SUBMITTED BY:
NILIMA NAYAK
017BSCIT027
2nd year/ 4th sem

Signature
Er. Sarjan Shrestha
(Lecturer)

Department of Computer Science

SUBMITTED TO:
OBJECTIVE:
TO FAMILIARIZE WITH BETWEEN OPERATOR, SQL WILDCARD
(LIKE OPERATOR), GROUP BY, HAVING CLAUSE TO GROUP
RESULTS
THEORY:
BETWEEN operator
The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates. The BETWEEN operator is inclusive: begin and end values are included.
Syntax: SELECT column_name(s)

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

SQL Wildcard (LIKE operator)


The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
 % - The percent sign represents zero, one, or multiple characters
 _ - The underscore represents a single character

Syntax: SELECT column1, column2, ...

FROM table_name

WHERE columnN LIKE pattern;

Examples showing different LIKE operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or Finds any values that have "or" in any position
%'
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a_%_ Finds any values that start with "a" and are at least
%' 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"
GROUP BY
The GROUP BY Statement in SQL is used to arrange identical data into groups with the help
of some functions. i.e. if a column has same values in different rows then it will arrange these
rows in a group. The GROUP BY statement is often used with aggregate functions (COUNT,
MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
Syntax: SELECT column_name(s)
FROM table_name

WHERE condition

GROUP BY column_name(s)

ORDER BY column_name(s);

HAVING clause
The HAVING Clause enables to specify conditions that filter which group results appear in
the results. The HAVING clause places conditions on groups created by the GROUP BY
clause. The HAVING clause was added to SQL because the WHERE keyword could not be
used with aggregate functions.
Syntax: SELECT column_name(s)
FROM table_name

WHERE condition

GROUP BY column_name(s)

HAVING condition

ORDER BY column_name(s);
STATEMENT 1: FIND INFORMATION OF ALL EMPLOYEE WHOSE
EXTENSION STARTS WITH ‘X2’ AND EMPLOYEE NUMBER BETWEEN
1200 AND 1500. (EMPLOYEES TABLE)
SOURCE CODE:
CREATE DATABASE lab3;

USE lab3;

SELECT DATABASE();

SHOW TABLES;

SELECT *FROM employees

WHERE extension LIKE 'x2%' AND employeeNumber BETWEEN 1200 AND 1500;

OUTPUT:

STATEMENT 2: FIND THE SUM OF QUANTITY IN STOCK FOR


DIFFERENT CATEGORIES OF PRODUCTLINE FROM PRODUCT TABLE.
SOURCE CODE:
SELECT *FROM products;

SELECT SUM(quantityInStock), productLine

FROM products

GROUP BY productLine;

OUTPUT:
STATEMENT 3: DETERMINE THE TOTAL NUMBER OF ORDERS FOR
STATUS (‘CANCELLED’, ’DISPUTED’, ’IN PROCESS’, ’ON HOLD’).
(ORDER TABLE)
SOURCE CODE:
SELECT *FROM orders;

SELECT COUNT(*),STATUS

FROM orders

WHERE STATUS IN ('Cancelled' ,'Disputed' , 'In Process' , 'On Hold')

GROUP BY STATUS;

OUTPUT:

STATEMENT 4: DETERMINE THE NUMBER OF EMPLOYEES AS PER


THE JOB TITLE. (EMPLOYEES TABLE)
SOURCE CODE:
SELECT *FROM employees;

SELECT COUNT(*), jobTitle

FROM employees

GROUP BY jobTitle;
OUTPUT:

STATEMENT 5: FIND THE CUSTOMER NUMBERS WITH SUM OF


AMOUNT OF PAYMENT IS GREATER THAN 200000. (PAYMENTS TABLE)
SOURCE CODE:
SELECT *FROM payments;

SELECT SUM(amount), customerNumber

FROM payments

GROUP BY customerNumber

HAVING SUM(amount) > '200000';

OUTPUT:

STATEMENT 6: LIST THE NAME OF CITIES WHERE CUSTOMER


NUMBER IS GREATER THAN 2. (CUSTOMERS TABLE)
SOURCE CODE:
SELECT *FROM customers;

SELECT COUNT(*), city

FROM customers

GROUP BY city
HAVING COUNT(*) > '2';

OUTPUT:

You might also like