Unit 2: Database Query Using SQL Syllabus: Single Row Functions
Unit 2: Database Query Using SQL Syllabus: Single Row Functions
Unit 2: Database Query Using SQL Syllabus: Single Row Functions
● Text functions: UCASE ()/UPPER (), LCASE ()/LOWER (), MID ()/SUBSTRING ()/SUBSTR(),
LENGTH (), LEFT (), RIGHT (), INSTR (), LTRIM (), RTRIM (), TRIM ().
● Date Functions: NOW (), DATE (), MONTH (), MONTHNAME (), YEAR (), DAY (),
DAYNAME ().
● Aggregate Functions: MAX (), MIN (), AVG (), SUM (), COUNT (); using
COUNT (*).
● Querying and manipulating data using Group by, Having, Order by.
COUNT Function
The COUNT function returns the total number of values in the specified field. It
works on both numeric and non-numeric data types. All aggregate functions by
default exclude nulls values before working on the data.
COUNT (*) is a special implementation of the COUNT function that returns the
count of all the rows in a specified table. COUNT (*) also considers Nulls and
duplicates.
mysql> select count(city) from empl;
| count(city) |
---------------
6
Using DISTINCT
Distinct includes only distinct values in the column
mysql> select count(distinct city) from empl;
| count(distinct city) |
--------------------------
3
------------------------
mysql> select count(*) from empl;
count(*)
--------------------------
6
2) SUM:-returns the sum of all the values in the specified column. SUM works on
numeric fields only. Null values are excluded from the result returned.
mysql> select sum(salary ) from empl;
sum(salary)
67000
3) AVG:-returns the average of the non NULL values in a specified
column.It works only on numeric data types.
mysql> select avg(salary) from empl;
avg(salary)
-----------------------
11166.6667
---------------
4) MIN:-Return the lowest value (minimum) in a set of non-NULL values.
mysql> select min(salary) from empl;
---------------
min(salary)
---------------
8000
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.
ORDER BY Clause
ORDER BY clause is used to display data in an ordered (arranged) form with respect to a
specified column. By default, ORDER BY displays records in ascending order of the specified
column’s values. To display the records in descending order, the DESC (means descending)
keyword needs to be written with that column.
SELECT *FROM empl WHERE salary BETWEEN 8000 and 12000 order by salary;
SELECT *FROM empl WHERE salary BETWEEN 8000 and 12000 order by salary ASC;
SELECT *FROM empl WHERE salary BETWEEN 8000 and 12000 order by salary DESC;
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
HAVING
The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
mysql> SELECT CITY,SUM(SALARY)
FROM EMPL
GROUP BY CITY
HAVING SUM(SALARY) > 20000
ORDER BY SALARY;
-------------------------
| CITY | SUM(SALARY) |
-------------------------
| vellore | 23000 |
| chennai | 27000 |
-------------------------