MySQL SQL Elements Explained
MySQL SQL Elements Explained
LANGUAGE (SQL)
ELEMENTS OF MYSQL
ELEMENTS OF MYSQL
The MYSQL implementation of SQL has certain elements that plays
an important role in defining/ querying a database. Some MYSQL basic
elements are…
MYSQL
Elements
Data
Literals Nulls Comments
Types 3
ELEMENTS OF MYSQL
A. Literals:
Literals, in general, refer to fixed data value. This fixed value may be
of character or numeric literals e.g. ‘Synthia’, 8, 25.5, ‘Raj Singh’ etc.
the character literals are always enclosed with in single quotes. a text
literal can have maximum length of 4000 bytes where as a numeric
literal can store a maximum of 53 digits of precision.
3 is a numeric 4
literal
ELEMENTS OF MYSQL
5
ELEMENTS OF MYSQL
B. Data Types:
Data Types in MYSQL are used to assign the proper types of the
attributes (columns) at the time of creating relation(table). We can
choose the data types for an attribute based on the values we want
to store in it. MYSQL offers the following set of Data Types.
Data
Types
Date and
Numeric String
Time
6
ELEMENTS OF MYSQL
B-1. Numeric:
This type of data type is used to store the numbers as a value with in
an attribute of a relation. A numbers (value) can be either with
fraction and without fractional part. In MYSQL the following data
types supports attribute’s value as numbers.
Sr No Data Type Description
1 INT Occupies 4 bytes per Cell
2 TINYINT Occupies 1 byte per Cell
3 SMALLINT Occupies 2 bytes per Cell
4 MEDIUMINT Occupies 3 bytes per Cell
5 BIGINT Occupies 8 bytes per Cell
6 FLOAT(P,S) Precision can be upto 24 places
7 DOUBLE (P,S) Precision can be upto 53 places
7
8 DECIMAL(P,S) Precision can be upto 53 places
ELEMENTS OF MYSQL
e.g.
if we take float(10,4) or double(10,4) or decimal(10,4) then 10 is
Precision (total number of digits to be stored) and 4 is scale
(total number of digits supported after decimal point).
8
ELEMENTS OF MYSQL
B-2. Date and Time:
This type of data type is used to store the Date and/ or Time as a
value with in an attribute of a relation. In MYSQL the following data
types supports attribute’s value as Date and/ or Time.
Sr No Data Type Description
1 DATE Stores date in YYYY-MM-DD format.
2 DATETIME Stores date and time in YYYY-MM-DD
HH:MM:SS format.
3 TIMESTAMP Stores date and time in YYYYMMDDHHMMSS
format
4 TIME Stores time in HH:MM:SS format
5 YEAR Stores year in 2-digit or 4-digit format.
9
ELEMENTS OF MYSQL
B-3. String:
This type of data type is used to store the numbers as a text with in
an attribute of a relation. A texts (value) should be specified with in
single or double quotes. In MYSQL the following data types store
strings.
Sr No Data Type Description
1 CHAR(SIZE) A Fixed Length string between 1 to 255 characters. By default only 1
char per cell.
2 VARCHAR(SIZE) A Variable Length string between 1 to 255 or 65535 characters
depending upon MYSQL version. Length should be defined when
declaring it as varchar.
3 BLOB or TEXT It can store maximum 65635 characters. It is referred to binary large
objects.
4 TINYBLOB or It can store maximum 255 characters. It is referred to tiny binary large
TINYTEXT objects.
5 MEDIUMBLOB or It can store maximum 16777215 characters. It is referred to medium
MEDIUMTEXT binary large objects. 10
6 LONGBLOB or It can store maximum 4294967295 characters. It is referred to Long
LONGTEXT binary large objects.
ELEMENTS OF MYSQL
Char Varchar
A Fixed Length string between 1 to 255 characters. A Variable Length string between 1 to 255 or
By default only 1 char per cell. 65535 characters depending upon MYSQL
version. Length should be defined when
declaring it as varchar.
When we use char with size 10 characters per cell, When we use varchar with size 10 characters
and specify lesser number characters as a value of per cell, and specify lesser number characters as
cell then also cell will occupy 10 characters by a value of cell then cell will occupy specified
suffixing trailing spaces. characters instead of suffixing trailing spaces.
11
ELEMENTS OF MYSQL
C. Null Values:
Null Values are the special type of values that indicates to EMPTY
Cells. The null values of a attribute can be avoided using NOT
NULL constraint. Null value can be replaced but it can not be
processed. Any arithmetic expression containing null, always
evaluates to null e.g. null + 12 = null, null*45=null etc.
Null values.
12
ELEMENTS OF MYSQL
D. Comments:
Comment is a text that is not executed instead of this it is used as an
internal document of your SQL command. This text will be helpful to
the users who wants to understand your SQL command. MYSQL
supports two types of comments namely single line and multi line.
The single line comments starts with either - - or # where as multi line
comments start with /* and ends with */
Multiline
comments…
13
SQL COMMANDS
MYSQL DATA DEFINITION LANGUAGE COMMANDS
1. To Create a New Database in MYSQL:
The Create Database command in MYSQL is used to create a new
database in MYSQL Server. This command accepts the name of the
database and creates it. If a database already exists then MYSQL
will report an error. The SYNTAX of this command is Create
Database <DatabaseName>;
A New Database
namely
MYTestDatabase
is created.
15
MYSQL DATA DEFINITION LANGUAGE COMMANDS
MYTestDatabase
is listed with the
list of database.
16
MYSQL DATA DEFINITION LANGUAGE COMMANDS
Naming Convention In MYSQL:
In computer programming, a naming convention is a set of rules for
choosing the character sequence to be used for identifiers like
DATABSE, TABLE, COLUMN or some other database object.
General Rules:
❑ Using lowercase will help speed typing, avoid mistakes as
MYSQL is case sensitive for Names.
❑ Using space between words is not allowed.
❑ While naming, it is essential that it starts with alphabet _ or $,
numbers are allowed from 2nd place onwards.
❑ Names should be a descriptive of the elements. i.e. — Self-
explanatory and not more than 64 characters. 17
List of available
databases.
18
MYSQL DATA DEFINITION LANGUAGE COMMANDS
3. To Delete (Remove) Databases in MYSQL;
The Drop Database command in MYSQL is used to delete and
existing database in MYSQL Server. The SYNTAX of this command
is Drop Database <DatabaseName>; or Drop Database If
Exists <DatabaseName>; Once a database is dropped then its all
associated tables and data will be destroyed and it will never be
restored.
Database is
dropped.
19
MYSQL DATA DEFINITION LANGUAGE COMMANDS
Dropped database
will no longer
available in
MYSQL.
20
MYSQL DATA DEFINITION LANGUAGE COMMANDS
4. To Open a Databases in MYSQL;
The Use command in MYSQL is used to open and existing database
in MYSQL Server. The SYNTAX of this command is Use
<DatabaseName>. Once a database in opened that it is ready for all
DML commands or in other words all DML command will be executed
on the tables available in it. If a database is not opened the MYSQL
will report and error.
db1 database is
opened.
21
MYSQL DATA DEFINITION LANGUAGE COMMANDS
MYSQL reports an
error because
database it not
opened.
22
MYSQL DATA DEFINITION LANGUAGE COMMANDS
5. To List available tables in Database in MYSQL;
The show tables command in MYSQL is used to list existing tables
with in opened database. The SYNTAX of this command is show
tables;
List of tables
available in cce
database.
23
MYSQL DATA DEFINITION LANGUAGE COMMANDS
6. To Create a Table in Database in MYSQL;
The Create Table command in MYSQL is used to create a new table
with in opened database. The SYNTAX of this command is
Create Table <TableName>
(
ColName_1 Datatype (Size) Column Level Constraint,
ColName_2 Datatype (Size) Column Level Constraint,
ColName_3 Datatype (Size) Column Level Constraint,
.
.
.
24
ColName_N Datatype (Size) Column Level Constraint
);
MYSQL DATA DEFINITION LANGUAGE COMMANDS
Student table
created in Test
Database
25
MYSQL DATA DEFINITION LANGUAGE COMMANDS
Marks table
created in Test
Database
26
MYSQL DATA DEFINITION LANGUAGE COMMANDS
7. To Create a Table with Column Level Primary Key Constraint:
The Primary Key constraint is a rule in table that allows you to
identify tuples uniquely. When we apply a primary key constraint to a
column then in future that column will not allow duplication as well as
value should be compulsorily entered at the time of inserting or
updating a tuple. There will be only one Column Level Primary Key
Constraint per table.
Create Table <TableName>
(
ColName_1 Datatype (Size) Primary Key,
ColName_2 Datatype (Size) Column Level Constraint,
.
27
ColName_N Datatype (Size) Column Level Constraint
);
MYSQL DATA DEFINITION LANGUAGE COMMANDS
Student table
created in Test
Database with
column level
primary key 28
MYSQL DATA DEFINITION LANGUAGE COMMANDS
29
MYSQL DATA DEFINITION LANGUAGE COMMANDS
If we want multiple primary keys on a table then we should use table level
primary key instead of column level.
Student is created
using multiple
primary keys on
different columns.
30
MYSQL DATA DEFINITION LANGUAGE COMMANDS
8. To Create a Table with Unique Constraint:
The Unique constraint is a rule in table that allows you to identify
tuples uniquely, this feature is similar to primary key but unique key
will accept null values where primary key will not allow null values.
When we apply a unique constraint to a column then in future
that column will not allow duplication but it allows null values. Unique
constraint can be applied on more than one columns.
Create Table <TableName>
(
ColName_1 Datatype (Size) Primary Key,
ColName_2 Datatype (Size) unique,
31
ColName_N Datatype (Size) unique
);
MYSQL DATA DEFINITION LANGUAGE COMMANDS
32
MYSQL DATA DEFINITION LANGUAGE COMMANDS
33
MYSQL DATA DEFINITION LANGUAGE COMMANDS
9. To Create a Table with Not Null Constraint:
The Not Null constraint is a rule in table that allows you to avoid null
values (empty cells) with in column at the time or inserting or
updating it. A table can contain any number of columns with Not Null
constraint. A not null and unique constraint can be combined
and applied on column. If we apply the not null and unique constraint
combination to a column than that column will not allow duplicate
as well as it will also not allow null values.
Create Table <TableName>
(
ColName_1 Datatype (Size) not null,
ColName_2 Datatype (Size) not null unique,
34
ColName_N Datatype (Size) unique
);
MYSQL DATA DEFINITION LANGUAGE COMMANDS
35
MYSQL DATA DEFINITION LANGUAGE COMMANDS
36
MYSQL DATA DEFINITION LANGUAGE COMMANDS
10. To Create a Table with Default Constraint:
The Default constraint is a rule in table that allows you to avoid null
values (empty cells) by setting default value to null values in a
column. A table can contain any number of columns with default
constraint. Default value will be inserted automatically when a
column is left blank during inserting a new record.
Create Table <TableName>
(
ColName_1 Datatype (Size) default <Default Value>,
ColName_2 Datatype (Size) default <Default Value>,
.
ColName_N Datatype (Size) default <Default Value> 37
);
MYSQL DATA DEFINITION LANGUAGE COMMANDS
38
MYSQL DATA DEFINITION LANGUAGE COMMANDS
11. To Create a Table with Check Constraint:
The Check constraint is a rule in table that allows you to avoid those
tuples which doesn’t satisfy the given condition. A table can contain
any number of columns with check constraint.
Create Table <TableName>
(
ColName_1 Datatype (Size) check (Condition),
ColName_2 Datatype (Size) check (Condition),
.
ColName_N Datatype (Size) check (Condition)
); 39
MYSQL DATA DEFINITION LANGUAGE COMMANDS
40
MYSQL DATA DEFINITION LANGUAGE COMMANDS
41
MYSQL DATA DEFINITION LANGUAGE COMMANDS
12. To Create a Table with Foreign Key Constraint:
The Foreign Key constraint is a non-key attribute whose values are
derived from the Primary Key of some other table. A table can have
many numbers of Foreign Keys on different columns.
44
MYSQL DATA DEFINITION LANGUAGE COMMANDS
45
MYSQL DATA DEFINITION LANGUAGE COMMANDS
46
MYSQL DATA DEFINITION LANGUAGE COMMANDS
When we interlink two tables using primary Options Description
key and foreign key constraints, it associate CASCADE If Primary Key Column
the problem/ concept of referential integrity. A value is updated or
deleted then Foreign
referential integrity is a system of rules that a Key Column Value will
DBMS uses to ensure that relationships also gets updated or
deleted
between records in related tables are valid, SET NULL If Primary Key Column
and that users don’t accidentally delete or value is updated or
change the related data. This rule states that deleted then Foreign
Key Column Value will
when a primary key column’s value gets set to null
updated/changed or deleted than what NO If Primary Key Column
ACTION value is updated or
should be done for foreign key table records. deleted then Foreign
This problems of referential integrity can be Key Column Value will
remain as it is
solved at the time of creating a foreign table.
RESTRICT This will reject updating
At the time of creating foreign key we should 47
or deleting Primary Key
suffix the statement as ON DELETE and/or Column’ Value
49
MYSQL DATA DEFINITION LANGUAGE COMMANDS
50
MYSQL DATA DEFINITION LANGUAGE COMMANDS
A foreign key can be named for a table, and the name applied to a
foreign key will be used to refer that foreign key, as well as it can also
be used to delete or modify the relationship.
Create Table <ForeignTableName>
(
ColName_1 Datatype (Size) Primary Key,
ColName_N Datatype (Size),
Constraint <Name> Foreign Key (Foreign Col) references
<SameTableName> (Primary Col)
);
51
MYSQL DATA DEFINITION LANGUAGE COMMANDS
52
MYSQL DATA DEFINITION LANGUAGE COMMANDS
13. To Modify the Structure of Table Using Alter Table Command:
The Alter Table command is used to modify the structure of existing
table. This command is used to ADD, MODIFY, CHANGE (Column
Name) and DELETE columns/ constraints of existing table.
To Add New Column
Alter Table <TN> ADD <NEW CN> <DataType> (Size) constraint First|
After Column Name;
To Change Column Data Type And Size
Alter Table <TN> MODIFY <OLD CN> <DataType>(Size) constraint;
To Change Name Of Column and Data Type And Size
Alter Table <TN> CHANGE <OLD CN> <NEW CN> <DataType>(Size)
constraint;
To Remove Column
Alter Table <TN> DROP <OLD CN>;
MYSQL DATA DEFINITION LANGUAGE COMMANDS
54
MYSQL DATA DEFINITION LANGUAGE COMMANDS
57
MYSQL DATA DEFINITION LANGUAGE COMMANDS
58
MYSQL DATA DEFINITION LANGUAGE COMMANDS
59
MYSQL DATA DEFINITION LANGUAGE COMMANDS
60
MYSQL DATA DEFINITION LANGUAGE COMMANDS
61
MYSQL DATA DEFINITION LANGUAGE COMMANDS
62
MYSQL DATA DEFINITION LANGUAGE COMMANDS
63
MYSQL DATA DEFINITION LANGUAGE COMMANDS
64
MYSQL DATA DEFINITION LANGUAGE COMMANDS
65
MYSQL DATA DEFINITION LANGUAGE COMMANDS
66
MYSQL DATA DEFINITION LANGUAGE COMMANDS
14. To Remove / Delete a Table Command:
The Drop Table command is used to remove a table from the
database. Once a table is removed from the database it can not be
restored. A dropped table will be removed from the database along
with its structure and records. The SYNTAX of this command is as
follows. If a table being dropped has foreign table or it doesn’t exists
then MYSQL will report an error.
Or
67
Drop Table If Exists <TableName>;
MYSQL DATA DEFINITION LANGUAGE COMMANDS
69
MYSQL DATA DEFINITION LANGUAGE COMMANDS
70
MYSQL DATA DEFINITION LANGUAGE COMMANDS
16. To Describe the Structure of a Table :
The Desc or Describe command used to view the structure of
existing able. The result of this command will show you the column
names, types, sizes and constraint used. The SYNTAX of this
command is as follows.
Desc <TableName>;
or
Describe <TableName>;
71
MYSQL DATA DEFINITION LANGUAGE COMMANDS
72
MYSQL DATA DEFINITION LANGUAGE COMMANDS
Describe command
shows the structure of
a table student
73
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
17. To Insert a record in a Table :
The Insert into command used to add a new record in a table. This
command accept the values of different columns and inserts a new
tuple in a table. There are two forms of insert into command, one
accepts the values of columns in the same order as the order of
columns in a table and second accept the column names and
corresponding values. In second form of insert into the order of the
column can be changed as well as we can omit values for some
columns. If we omit some columns from insert into command the null
or default value will be inserted. At the time of writing values the char,
varchar and date type values should be enclosed in single
quotes. The SYNTAXs of this command is as follows…
Insert into <TableName> Values (Val1, Val2,…, ValN); Or 74
RELATION: Student
inserted
75
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Insert Into Student Values('102', 'Rahul Raj', '17', 'M','1992-01-
02'); executed in MYSQL will be
record when
RELATION: Student
inserted
RELATION: Student
RELATION: Student
inserted
RELATION: Student
record
RELATION: Student
record
RELATION: Student
82
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
83
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
84
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Or
102 Rahul
XYZRaj 17 M 1992-01-02
103 NULL
XYZ 18 F 1992-05-04
This command
104 HeetXYZ
Parmar 18 M 1998-10-09 will change all
values of name
105 Rajvi
XYZ Patel 17 F 1997-10-09 columns as XYZ
NULL Radha
XYZPatel 17 F 1996-06-09
103 NULL 18
19 F 1992-05-04
This command
104 Heet Parmar 18
19 M 1998-10-09 will change all
values of Age
105 Rajvi Patel 17
18 F 1997-10-09 column by
NULL Radha Patel 17
18 F 1996-06-09
increasing 1.
Or
102
103 Rahul
NULL Raj 17
18 M
F 1992-01-02
1992-05-04
103
104 Heet Parmar
NULL 18 M
F 1992-05-04
1998-10-09
This command
104 Heet Parmar 18 M 1998-10-09 will remove only
those records
105 Rajvi Patel 17 F 1997-10-09 (tuples) of table
NULL Radha Patel 17 F 1996-06-09
(relation) student
whose Name
starts with R
102
104 Heet
RahulParmar
Raj 17
18 M 1992-01-02
1998-10-09
103
105 Rajvi
NULL Patel 18
17 F 1992-05-04
1997-10-09 This command
NULL
104 Heet
RadhaParmar
Patel 18
17 M
F 1998-10-09
1996-06-09
will remove only
those records
105 Rajvi Patel 17 F 1997-10-09 (tuples) of table
(relation) student
NULL Radha Patel 17 F 1996-06-09 whose DOB is
before 3rd May
1992.
Delete From Student Where DOB < ‘1992-05-03’;
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Order Of Clauses
In
SELECT Command
113
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
19-1. To Select All Column and All Rows from a Table :
To select all column and all rows from a table we should use Select
command in following format…
Or
114
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT;
NULL Radha Patel 17 F 1996-06-09
119
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT ROLLNO, NAME,
NULL Radha Patel 17 F 1996-06-09
AGE FROM STUDENT;
Gender DOB
F 1990-01-02
M 1992-01-02
F 1992-05-04
M 1998-10-09
F 1997-10-09
122
F 1996-06-09
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
126
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT GENDER FROM
NULL Radha Patel 17 F 1996-06-09
STUDENT;
Gender
F
M
F
M
F
127
F
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Gender
F
M
129
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Name Age
Meeti Sheth 16
Rahul Raj 17
NULL 18
Heet Parmar 18
Rajvi Patel 17
133
Radha Patel 17
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
135
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
136
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
19-4. To Select Specific / All Columns but Selected Rows from a Table
:
To select specific/ all columns and specific rows then our select
command should use WHERE CLAUSE and a condition in following
format…
144
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
146
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
147
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE AGE=16 OR AGE=18;
148
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
152
RELATION: Student
Roll No Name Age Gender FEES
101 Meeti Sheth 16 F 4000
102 Rahul Raj 17 M 5000
103 NULL 18 F 6000
104 Heet Parmar 18 M 5000
105 Rajvi Patel 17 F 2000
SELECT FEES, FEES*0.15
NULL Radha Patel 17 F 9000
FROM STUDENT;
5000 750
6000 900
5000 750
9000 1350
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
FEES FEES/30
4000 133.33
5000 166.66
6000 200.00
5000 166.55
2000 66.66
155
9000 300.00
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
157
RELATION: Student
Roll No Name Age Gender FEES
101 Meeti Sheth 16 F 4000
102 Rahul Raj 17 M 5000
103 NULL 18 F 6000
104 Heet Parmar 18 M 5000
105 Rajvi Patel 17 F 2000
SELECT FEES, FEES/30 AS
NULL Radha Patel 17 F 9000
‘DAILY FEES’ FROM
STUDENT;
9000 300.00
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
5000 750
6000 900
5000 750
NULL NULL
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Scalar Expression is
renamed as 15% OF 161
Salary
RELATION: Student
Roll No Name Age Gender FEES
101 Meeti Sheth 16 F 4000
102 Rahul Raj 17 M 5000
103 NULL 18 F 6000
104 Heet Parmar 18 M 5000
105 Rajvi Patel 17 F 2000
SELECT NAME AS ‘STUDENT
NULL Radha Patel 17 F 9000
NAME’ , AGE AS ‘STUDENT
AGE’ FROM STUDENT;
Radha Patel 17
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
164
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT NAME, ’HAS’ , AGE
NULL Radha Patel 17 F 1996-06-09
FROM STUDENT;
175
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
176
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
19-8. A condition based on a List of Values:
The IN keyword is used to specify the list of values, from which if
record match a value then it will be considered form output. This
operator works for any data types i.e. either number, date, time or
string.
178
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
179
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE AGE = 16 OR
AGE=18 );
180
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
Replacement to In
keyword.
181
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE AGE NOT IN (16,18 );
182
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
183
Displays the list of record
which is not in a list.
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE GENDER IN (‘F’);
185
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
19-8. Matching a Pattern for a String:
The LIKE keyword is used to match pattern for a string type column.
The LIKE keyword offers two wild card characters namely % and _.
The % can matches zero or more then zero characters(any length of
characters) underscore matches any character. This pattern
matching using LIKE operator is useful in matching string patterns
like starting with specific letter(s), ends with specific letter(s), contains
specific letters(s).
187
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
188
RELATION:
Roll No NameStudent
Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE NAME NOT LIKE
‘R%’;
189
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
191
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
192
RELATION:
Roll No NameStudent
Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE NAME LIKE ‘%L%;
193
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
195
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
197
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
198
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
19-9. Searching For Nulls:
The IS keyword is used to match the null (Empty Cells) values. As a
null is not a value, it can not be compared using = sign. Therefore,
the IS keyword is used to match the null values.
200
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
201
RELATION:
Roll No NameStudent
Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
WHERE NAME IS NOT NULL;
203
Displays a list of record in
which ssn is not NULL
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
19-10. Sorting Result Set:
The Select command in MYSQL will return the RESULT SET. The
records in the RESULT SET can be arranged in ascending or
descending order using ORDER BY CLAUSE. This CALUSE accept
one or two column names and sort the RESULT SET in ascending
order by default. If we want to arrange the RESULT SET in
descending order then we need to suffix the keyword desc. If we
specify only one column in ORDER BY CALUSE it is known as
SINGLE SORTING, and if we specify multiple columns then it is
referred to as MULTIPLE SORTING or NESTED SORTING.
ASCENDING 104
105
Heet Parmar
Rajvi Patel
18
17
M
F
1998-10-09
205
1997-10-09
SINGLE SORTING
Roll No Name Age Gender DOB
ON 101 Meeti Sheth 16 F 1990-01-02
SINGLE SORTING
ON
Roll No Name Age Gender DOB
ROLLNO COLUMN 105 Rajvi Patel 17 F 1997-10-09
104 Heet Parmar 18 M 1998-10-09
IN 103 NULL 18 F 1992-05-04
DESCENDING 102
101
Rahul Raj
Meeti Sheth
17
16
M
F
1992-01-02
207
1990-01-02
ORDER NULL Radha Patel 17 F 1996-06-09
RELATION: Student
Roll No Name Age Gender DOB
101 Meeti Sheth 16 F 1990-01-02
102 Rahul Raj 17 M 1992-01-02
103 NULL 18 F 1992-05-04
104 Heet Parmar 18 M 1998-10-09
105 Rajvi Patel 17 F 1997-10-09
SELECT * FROM STUDENT
NULL Radha Patel 17 F 1996-06-09
ORDER BY NAME DESC;
SINGLE SORTING
Roll No Name Age Gender DOB
ON 105 Rajvi Patel 17 F 1997-10-09
220
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
SELECT DEPARTMENT_ID, SUM(SALARY) FROM EMP GROUP BY DEPARTMENT_ID
HAVING DEPARTMENT_ID<40;
221
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
225
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
233
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
234
235
236
MYSQL’S
BUILT-IN-FUNCTIONS
MYSQL BUILT IN FUNCTIONS
A Function is a special type of predefined command set that per-
forms some operation and returns a single or multiple value(s). In MYSQL
the functions are categorized as follows…
MYSQL
Functions
Multiple
Single Row
Row (Group/
(Scalar) 238
Aggregate)
MYSQL BUILT IN FUNCTIONS
A. Group/ Aggregate/ Multiple Row Functions:
In MYSQL, This types of functions accepts a column, i.e. multiple
rows (values) and return the summery of multiple value i.e.
aggregated value from multiple rows. All aggregate functions
generally works for numeric type only. This includes the following
functions…
Aggregate
Functions
240
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
SUM(FEES)
SELECT SUM(FEES) FROM STUDENT;
21500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
SUM(ALL FEES)
SELECT SUM(ALL FEES) FROM STUDENT;
21500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
SUM(DISTINCT FEES)
SELECT SUM(DISTINCT FEES) FROM STUDENT;
16500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
SUM(DISTINCT SUM(ALL
SELECT SUM(DISTINCT FEES), SUM(ALL FEES) FEES) FEES)
FROM STUDENT; 16500 21500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
SUM(DISTINCT SUM(ALL
SELECT SUM(DISTINCT FEES), SUM(ALL FEES) FEES) FEES)
FROM STUDENT WHERE AGE<=17; 10500 10500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AGE SUM(FEES)
SELECT AGE, SUM(FEES) FROM STUDENT
16 4000
GROUP BY AGE;
17 6500
18 11000
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
247
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
248
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
249
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
250
MYSQL BUILT IN FUNCTIONS
A.2 MAX() Function:
This function accept the column of a table and return the maximum of
all values of a column. If in a query a where condition is used then
only selected values of a column will be will be considered and
maximum will be returned. This functions works with numeric and
date time columns, for a date time column it returns the most recent
date time as MAX date e.g. if we apply this functions on today’s date
and yesterday’s date then it returns today’s date. The SYNTAX to use
this function is as follows…
MAX(FEES)
SELECT MAX(FEES) FROM STUDENT;
6000
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MAX(DOB)
SELECT MAX(DOB) FROM STUDENT;
1998-10-09
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MAX(DISTINCT FEES)
SELECT MAX(DISTINCT FEES) FROM STUDENT;
6000
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MAX(FEES) MAX(DOB)
SELECT MAX(FEES), MAX(DOB) FROM STUDENT;
6000 1998-10-09
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MAX(FEES) MAX(DOB)
SELECT MAX(FEES), MAX(DOB) FROM STUDENT
5000 1997-10-09
WHERE AGE<=17;
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AGE MAX(FEES)
SELECT AGE, MAX(FEES) FROM STUDENT
16 4000
GROUP BY AGE;
17 5000
18 6000
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
258
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
259
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
260
MYSQL BUILT IN FUNCTIONS
A.3 MIN() Function:
This function accept the column of a table and return the minimum of
all values of a column. If in a query a where condition is used then
only selected values of a column will be will be considered and
minimum will be returned. This functions works with numeric and
date time columns, for a date time column it returns the oldest date
time as MIN date e.g. if we apply this functions on today’s date and
yesterday’s date then it returns yesterday date. The SYNTAX to use
this function is as follows…
MIN(FEES)
SELECT MIN(FEES) FROM STUDENT;
1500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MIN(DOB)
SELECT MIN(DOB) FROM STUDENT;
1990-01-02
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MIN(DISTINCT FEES)
SELECT MIN(DISTINCT FEES) FROM STUDENT;
1500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MIN(FEES) MIN(DOB)
SELECT MIN(FEES), MIN(DOB) FROM STUDENT;
1500 1990-01-02
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
MIN(FEES) MIN(DOB)
SELECT MIN(FEES), MIN(DOB) FROM STUDENT
1500 1990-01-02
WHERE AGE<=17;
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AGE MIN(FEES)
SELECT AGE, MIN(FEES) FROM STUDENT GROUP
16 4000
BY AGE;
17 1500
18 5000
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
268
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
269
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
270
MYSQL BUILT IN FUNCTIONS
A.4 AVG() Function:
This function accept the column of a table and return the
average(mean) of all values of a column. An average of values will be
calculated as addition of values divided by total number of values. If
in a query a where condition is used then only selected values of a
column will be will be considered and average will be returned. This
function will ignore NULL cells i.e. if a columns contains 4 values out
of which one value is NULL then average will be calculated as
addition of 3 values divided by 3. The SYNTAX to use this function is
as follows…
AVG(FEES)
SELECT AVG(FEES) FROM STUDENT;
4300
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AVG(ALL FEES)
SELECT AVG(ALL FEES) FROM STUDENT;
4300
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AVG(DISTINCT FEES)
SELECT AVG(DISTINCT FEES) FROM STUDENT;
4125
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AVG(DISTINCT AVG(ALL
SELECT AVG(DISTINCT FEES), AVG(ALL FEES) FEES) FEES)
FROM STUDENT; 4125 4300
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AVG(DISTINCT AVG(ALL
SELECT AVG(DISTINCT FEES), AVG(ALL FEES) FEES) FEES)
FROM STUDENT WHERE AGE<=17; 3500 3500
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
AGE AVG(FEES)
SELECT AGE, AVG(FEES) FROM STUDENT GROUP
16 4000
BY AGE;
17 3250
18 5500
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
278
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
279
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
280
MYSQL BUILT IN FUNCTIONS
A.5 Count() Function:
This function accept the column of a table and return the total
number of cells contains value(s) other than NULL. If in a query a
where condition is used then only selected values of a column will be
will be considered and count of values will be returned. For e.g. if a
column contains 4 values out of which 2 values are NULL then this
function returns 2 as result. This function will ignore NULL cells and
count. The SYNTAX to use this function is as follows…
281
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
COUNT(FEES)
SELECT COUNT(FEES) FROM STUDENT;
5
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
COUNT(ALL FEES)
SELECT COUNT(ALL FEES) FROM STUDENT;
5
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
COUNT(DISTINCT FEES)
SELECT COUNT(DISTINCT FEES) FROM
4
STUDENT;
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
286
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
287
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
288
MYSQL BUILT IN FUNCTIONS
A.5 Count(*) Function:
This function will count and return the total numbers of rows
available in a table. It has nothing to with NULL values stored in
table. The SYNTAX to use this function is as follows…
289
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
COUNT(*)
SELECT COUNT(*) FROM STUDENT;
6
MYSQL BUILT IN FUNCTIONS
RELATION: STUDENT
Roll No Name Age Gender DOB Fees
101 Meeti Sheth 16 F 1990-01-02 4000
102 Rahul Raj 17 M 1992-01-02 5000
103 NULL 18 F 1992-05-04 6000
104 Heet Parmar 18 M 1998-10-09 5000
105 Rajvi Patel 17 F 1997-10-09 1500
NULL Radha Patel 17 F 1996-06-09 NULL
COUNT(*) COUNT(FEES)
SELECT COUNT(*), COUNT(FEES) FROM
6 5
STUDENT;
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
292
MYSQL DATA MANIPULATION LANGUAGE COMMANDS
293
MYSQL’S JOINS
JOINS
A Join is a SELECT query (command) that combines rows from two
or more tables. In a join-query , more than one table are listed in from
clause. The function of combining multiple tables is called joining. The
SYNTAX of joining more than one table is as follows…
e.g.
This query will result a
SELECT * FROM emp, dept; Cross Join of emp and
dept table.
295
JOINS
RELATION: STUDENT RELATION: MARKS
ROLL NAME AGE GENDER DOB ROLLNO SUBJECT MARKSOBT
NO CODE
101 Meeti Sheth 16 F 1990-01-02 101 S1 59
Dept Table
Emp Table
300
JOINS
Dept Table
Emp Table
304
JOINS
Dept Table
Emp Table
308
JOINS
312
Identical Column
JOINS
RELATION: STUDENT RELATION: MARKS
ROLL NAME AGE GENDER DOB ROLLNO SUBJECT MARKSOBT
NO CODE
101 Meeti Sheth 16 F 1990-01-02 101 S1 59
Deptno is identical
column in both tables, it
shows the problem of
ambiguity as it is
mentioned without
prefixing table name.
317
Now, no problem of
ambiguity as identical
JOINS column is prefixed with
column name.
318
JOINS
RELATION: STUDENT RELATION: MARKS
ROLL NAME AGE GENDER DOB ROLLNO SUBJECT MARKSOBT
NO CODE
101 Meeti Sheth 16 F 1990-01-02 101 S1 59
322
JOINS
In MYSQL, two retrieve records from more than one table, a join
operation should be performed in select command. A MYSQL, offers
following types of joins…
Joins
Or