St. Xavier'S College: (Affiliated To Tribhuvan University) Maitighar, Kathmandu
St. Xavier'S College: (Affiliated To Tribhuvan University) Maitighar, Kathmandu
St. Xavier'S College: (Affiliated To Tribhuvan University) Maitighar, Kathmandu
XAVIER’S COLLEGE
(Affiliated to Tribhuvan University)
Maitighar, Kathmandu
SUBMITTED BY:
NILIMA NAYAK
017BSCIT027
2nd year/ 4th sem
Signature
Er. Sarjan Shrestha
(Lecturer)
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
FROM table_name
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;
WHERE extension LIKE 'x2%' AND employeeNumber BETWEEN 1200 AND 1500;
OUTPUT:
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
GROUP BY STATUS;
OUTPUT:
FROM employees
GROUP BY jobTitle;
OUTPUT:
FROM payments
GROUP BY customerNumber
OUTPUT:
FROM customers
GROUP BY city
HAVING COUNT(*) > '2';
OUTPUT: