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

MySQL - Impotant Clarifications

The document discusses various aggregate and scalar functions in MySQL like COUNT, SUM, AVG, MIN, MAX. It also covers topics like GROUP BY clause, SUBSTRING function, INSTR function, ROUND function and TRIM function.

Uploaded by

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

MySQL - Impotant Clarifications

The document discusses various aggregate and scalar functions in MySQL like COUNT, SUM, AVG, MIN, MAX. It also covers topics like GROUP BY clause, SUBSTRING function, INSTR function, ROUND function and TRIM function.

Uploaded by

Naseema Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MYSQL

Note :
• This material is just prepared keeping in mind the common mistakes made by the
students.
• It does not cover all the topics. only the topics which need clarifications are included

Functions are categorized as single row or scalar and group or multirow or aggregate functions
Aggregate function(important topic frequently asked)

aggregate functions namely;

1) COUNT
2) SUM
3) AVG
4) MIN
5) MAX

count()
• The COUNT() function is an aggregate function that returns the number of rows in a
table.
• The COUNT() function has three forms: COUNT(*), COUNT(col_name) and
COUNT(DISTINCT col_name).

Demo table( id and val)

COUNT(*) function

The COUNT(*) function returns the number of rows in a table.

The COUNT(*) returns the number of rows including duplicate, non-NULL and NULL rows.

• SELECT COUNT(*)FROMdemos;


• SELECT COUNT(*) FROM demos WHERE val = 2;

COUNT(col_name)

The COUNT(col_name) returns the number of rows that do not contain NULL values .

• SELECT COUNT(val) FROM demos;

COUNT(DISTINCT colname)

The COUNT(DISTINCT expression) returns the number of distinct rows that do not contain NULL
values as the result of the expression.
SELECT COUNT(DISTINCT val)FROM demos;

Other examples :
• SELECT sum(val) FROM demos;output : 18
• Select sum(distinct val) from demos : output :15
• SELECT COUNT(distinct val) FROM demos; output : 15

Note :TheCOUNT() function returns 0 if there is no matching row found.


Eg: if all the values in the “val” column of the demo table are null then
SELECT COUNT(val) FROM demos;output : 0
Apart from these other all functions are scalar functions

Topic II
Group by clause
General syntax:
Group by clause combines all those records that have identical values in a particular field.
When we perform group by all the records are divided into groups.

empno ename job mgr hiredate sal comm deptno

8369 Smith Clerk 8902 1990-12-18 800.00 NULL 20

8499 Anya Salesman 8698 1991-02-20 1600.00 300.00 30

8521 Seth Salesman 8698 1991-02-22 1250.00 500.00 30

8566 Mahadevan Manager 8839 1991-04-02 2985.00 NULL 20

8654 Momin Salesman 8698 1991-09-28 1250.00 1400.00 30

8698 Bina Manager 8839 1991-05-01 2850.00 NULL 30

8882 Shivansh Manager 8839 1991-06-09 2450.00 NULL 10

8888 Scott Analyst 8566 1992-12-09 3000.00 NULL 20

8839 Amir President NULL 1991-11-18 5000.00 NULL 10

8844 Kuldeep Salesman 8698 1991-09-08 1500.00 0.00 30

8886 Anoop Clerk 8888 1993-01-12 1100.00 NULL 20

9800 Jatin Clerk 8698 1991-12-03 950.00 NULL 30

8902 Fakir Analyst 8566 1991-12-03 3000.00 NULL 20

8934 Mita Clerk 8882 1992-01-23 1300.00 NULL 10

Eg. If I divide the records according to their deptno then I will get 3 groups i.e group 10 , 20, 30
The resultant query will have only one record from each group.
The column clause includes only the grouped column and any column with aggregate functions only.
Q1. Display the deptno and the number of employees in each deptno
• So it is clear that we need to group then so ,We can write
• Select ____________________ from emp group by deptno;
• And in the column clause required data is deptno ---- this is a grouped col
• And number of employees means the number of records in each group count(*)
• So the final query we can write as
Select deptno,count(*) from employee group by deptno;
Q2. Display the department no and total salary and average salary of each department.
Select deptno, sum(sal),avg(sal) from employee group by deptno;

Q3. Display salary, deptno department wise. --- wrong query


Select deptno , salary from employee group by deptno;
Deptno is a grouped column we can write but salary is non- group column and any non-group
column can be used with the aggregate function only
If the question is
Q3. Display the deptno and the total salary of each department
Select deptno, sum(sal) from employee group by deptno

Q4. Display designation and number of employees in each type of designation


Select job, count(*) from employee group by job;
In column clause job is a grouped column and count(*) is aggregate fn so it a valid one

Q5. Display the designation and the number of employees who are clerk and salesman
Select job, count(*) from employee group by job having job in(‘clerk’,’salesman’);
Select job, count(*) from employee where job in(‘clerk’,’salesman’) group by job ;
Both the queries are correct , appropriate is having clause because we are filter the group
Q6. Display the deptno and the number of employees of those dept with more than 3 employees
in each dept.
Select deptno , count(*) from employee where count(deptno)>3 group by deptno; is wrong
because Where clause cannot have aggregate function but having clause can contain either
simple expression or aggregate function and having is used to filter the records
Select deptno, count(*) from employee group by deptno having count(deptno)>3; is the correct

Note you will use joins only when the data to displayed is from two tables. But the above queries
data is from one table only so u don’t use joins at all
Q. Display the deptno,dname and the total salary of each department
Select e.deptno, d.dname , sum(sal) where e.deptno = d.deptno group by deptno;

Read the textbook thoroughly and also solve the exercise questions any doubts message me on
what app.

There are many ways to use data coming from multiple tables using the following:
• Cartesian product
• Union
• Intersection
• Joins
Refer to Appendix D in your text book for union and
intersection( it was asked in 4 years back paper)
This is similar to concept sets in math (AU B)..instead of
elements u have records.

Topic III
SUBSTRING (str,m[,n]) :

• Returns a portion of str, beginning at character m and n characters long. If m is positive, MYSQL
counts from beginning of str.
• If m is negative, MYSQL counts from backwards from the end of str.
• n is optional, if omitted returns all characters till the end , otherwise specified number of
characters
• If n is less than 1, null is returned

SELECTSUBSTRING('MySQL SUBSTRING',1,5); result : MySQL


SELECTSUBSTRING('MYSQL SUBSTRING',7); result : SUBSTRING
SELECTSUBSTRING('MYSQL SUBSTRING', 0); result : empty string
SELECTSUBSTRING('MySQL SUBSTRING',-15,5); result: MYSQL
SELECTSUBSTRING('MySQL SUBSTRING',-10); result : SUBSTRING
SELECTSUBSTRING('MYSQL SUBSTRING', 3,-1); result: empty string (null) because n is -1

Instr(ori_str,sub_str): MySQL INSTR() takes a string and a substring of it as arguments, and returns an
integer which indicates the position of the first occurrence of the substring within the string

If the sub sting is not present, then It will return 0

Round function(n[,m]): Returns value of argument n rounded to m places right of the decimal point. M
can be negative to round off digits left of the decimal point.
SELECT ROUND(1.23456789123456789, 8) ; Result:1.23456789
SELECT ROUND(125.315); Result: 125
SELECT ROUND(125.315, 0); Result: 125
SELECT ROUND(125.315, 1); Result: 125.3
SELECT ROUND(125.315, 2); Result: 125.32
SELECT ROUND(125.315, -1); Result: 130
SELECT ROUND(125.315, -2); Result: 100
SELECT ROUND(125.315, -3); Result: 0
SELECT ROUND(-125.315); Result: -125
trim(): trim([{both | Leading | Trailing | [remstr] from ] str)
Returns the string str with all remstr prefixes or suffixes removed.
If none of the specifiers BOTH, LEADING, or Trailing is given, BOTH is assumed

SELECT TRIM(BOTH '123' FROM '123Tech123');Result: 'Tech'


SELECT TRIM(LEADING '0' FROM '0001230'); Result: '1230'
SELECT TRIM(TRAILING '0' FROM '0001230'); Result: '000123'
SELECT TRIM('w' FROM ' welcomechirecw ‘);Result:elcomechirec // trims w
SELECT TRIM(' welcomechirec '); Result:welcomechirec // trims spaces

You might also like