03__MySQL-commands
03__MySQL-commands
Create a database:
Open a database:
Delete a database
Create table Command
Alter Table command is used to modify To change data type or modify size
the structure of table by modifying the
column definition of its column. It
perform following operations:
To add new column in table
To remove a column physically
To rename column
Structure after Modification
Name Purpose
SUM() Returns the sum of given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given
column.
SQL Commands
Aggregate Functions & NULL
Consider a table Emp having following records as-
Null values are excluded while (avg)aggregate function is
used Emp
Code Name Sal
E1 Mohak NULL
E2 Anuj 4500
E3 Vijay NULL
E4 Vishal 3500
E5 Anil 4000
SQL Queries Result of query
mysql> Select Sum(Sal) from EMP; 12000
mysql> Select Min(Sal) from EMP; 3500
mysql> Select Max(Sal) from EMP; 4500
mysql> Select Count(Sal) from 3
EMP; mysql> Select Avg(Sal) from 4000
EMP; mysql> Select Count(*) from 5
EMP;
SQL Commands
Aggregate Functions & Group
An Aggregate function may applied on a column with DISTINCT or
ALL keyword. If nothing is given ALL is assumed.
Using SUM (<Column>)
This function returns the sum of values in given column orexpression.
mysql> Select Sum(Sal) from EMP;
mysql> Select Sum(DISTINCT Sal) from EMP;
mysql> Select Sum ( S a l ) from EMP where
City=‘Jaipur ’;
mysql> Select Sum ( S a l ) from EMP Group By C i t y ;
mysql> Select Job, Sum(Sal) from EMP Group By Job;
Note :- Where clause works in respect of whole table but Having works on
Group only. If Where and Having both are used then Where will be
executed first.
SQL Commands
Ordering Query Result – ORDER BY Clause
A query result can be orders in ascending (A-Z) or
descending (Z-A)
order as per any column. Default is Ascending order.
mysql> SELECT * FROM Student ORDER BY City;
To get descending order use DESC key word.
mysql> SELECT * FROM Student ORDER BY City
DESC;
mysql> SELECT Name, Fname, City FROM Student
Where Name LIKE ‘R%’ ORDER BY Class;