MySQL - Impotant Clarifications
MySQL - Impotant Clarifications
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)
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).
COUNT(*) function
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 .
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
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.
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;
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
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
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