Structured Query Language (SQL)
Structured Query Language (SQL)
They are used to define the structure of the database objects i.e. all commands
required to create, modify or remove the existing structure comes under this
category.
For e.g.: DDL commands include CREATE DATABASE, CREATE TABLE, ALTER
TABLE and DROP TABLE.
These statements consist of a set of commands which are used to store, access and
manipulate data from existing tables.
For e.g. DML statements include INSERT, SELECT, UPDATE AND DELETE
commands.
Datatypes in MYSQL
Table is the basic entity of any database which is used to store information in rows
and columns. Each attribute/column of a table has a specific datatype.
Datatype refers to the type of value that a particular attribute of a table is
going to hold.
MYSQL offers three main datatypes:
• Numeric
• Date and Time
• String Types.
Let us now discuss them in detail.
Note: Character and Date values are always enclosed within single quotes
MYSQL Commands
SHOW DATABASES
This command is used to see the list of all the existing databases.
Syntax:
Show databases;
USE DATABASE
Use command is used to open any existing database so that the user can work
inside it.
Syntax:
Use <database name>;
Example:
The above command changes the current database to school.
SHOW TABLES
A database is a storehouse of tables. Show tables command is used to see the list
of tables present inside the current database.
Syntax:
Show tables;
Since, presently we are working in School database, it will display the list of tables
present inside school database.
CREATE DATABASE
Create database command is used to create a new database.
Syntax:
Create database <database name>;
Example:
Let’s create a new database company
CREATE TABLE command is used to create a new table inside the database. In this
command we define the complete structure of any table.
The CREATE TABLE command requires:
✓ Name of the table
✓ Names of fields ,their Datatypes and size(if any)
✓ Constraints(if any)
Syntax:
CREATE TABLE<tablename>
(<Fieldname _1> <datatype> [size] [constraints],
<Fieldname_ 2> <datatype> [size] [constraints],
.
.
.
.
.
<Fieldname_ n> <datatype>[size][constraints]
);
Note:
✓ [] means optional arguments.
✓ Every command in MYSQL ends with a semicolon.
Example:
Let’s create the following table:
Table: Student
Column Name Data Type Size
admno Integer 3
firstname Varchar 20
DOB Date
mobileno Varchar 10
Class Integer 2
section Char 1
Marks Decimal 3,1
A new table ‘Student’ will be created inside School database with the above
mentioned fields.
DESC command:
Desc or Describe command is used to see the structure of any existing table.
Syntax:
Desc/Describe <tablename>;
Command:
Till now we have studied different commands how to create a database and tables
inside it. Now let’s learn how we can insert records inside the table.
Example:
Let’s insert the following information inside the table student:
Command:
The above command can also be used to insert the first record in a table.
Subsequently, we can insert all the records in the table using this command.
Note: The columns for which value is not inserted will get NULL value.
Syntax:
Note:
Where clause is optional and is used to specify criteria.
<condition> is given according to the following syntax
<Fieldname> operator< value>;
Example
SELECT admno, firstname FROM student WHERE class=11;
Syntax:
SELECT * FROM <table name>;
Example: Display all the information of a table student.
Example: Display name and marks of all students from table Student.
Arithmetic operators:
Arithmetic operator takes two operands and performs a mathematical calculation
on them. However, they can be used only in SELECT command.
The arithmetic operators used in SQL are:
+ Addition
- Subtraction
* Multiplication
/ Division
Relational operators:
Relational operator is used along with ‘where clause’ to specify criteria. It is used to
compare the two operands.
Types of Relational operators are -
< Less than
<= less than or equal to
> Greater than
>= greater than or equal to
= equal to
! = or <> not equal to
Examples:
Consider Table Student
Admno Firstname DOB mobileno Class Section marks
100 Priyanka 2003-01-13 9811245387 11 A 76.5
123 Suman 2006-04-02 8576230122 6 C NULL
201 Madhav 2002-09-18 9711289701 10 D 69
345 Sambav 2000-08-17 9811456781 12 B 92
134 Madhukar 2007-09-09 9711542908 5 B 71
483 Kavya 2007-06-12 9810211345 5 A 53.5
235 Sanvi 2003-10-11 8657320134 9 D 94
1. Display names and marks of students whose score is greater than 75.
Logical operators:
Logical operators are used along with 'where clause' to join two or more conditions.
Logical operators are:
➢ AND-In case of ‘AND’ operator the record will be shown in the output only
when all the conditions joined by ‘AND’ evaluates to true.
➢ OR- In case of ‘OR’ operator, the record will be displayed in the output in
case any one condition joined by ‘OR’ evaluates to true.
➢ NOT-NOT operator is used to negate the condition.
Examples:
1. Display information of class 11th students who have scored less than 80
marks.
Note:
If we use ‘And’ operator instead of ‘Or’ we will get an empty set
because at one point of time student would be either in 11th or 12th.
We cannot use ‘And’ to join conditions based on same column.
3. Display name, class and section of all students who are not in section A.
LIKE OPERATOR
LIKE OPERATOR is used for pattern searching. This operator is used along with
wildcard characters.
MYSQL has two wild card characters:
➢ Percentage sign (%) – can be replaced by any number of characters (even
zero character).It can be any character from (a-z/A-Z) or number from (0-
9).
➢ Underscore ( _ ) – can be replaced by any single character or number.
Note:
In MySQL, Patterns are case-insensitive by default.
We do not use = or <> while performing pattern searching.
Example:
2. To display information about students whose name ends with alphabet ‘i’.
Range Operators
These operators are used to define range of values in condition.
IN Operator
The IN operator allows us to specify multiple values in a WHERE clause.
Syntax:
SELECT fieldname(s)
FROM table_name
WHERE fieldname IN (value1, value2, ...);
Example:
To display information about students studying in section A and D.
BETWEEN Operator
The BETWEEN operator is used to define the range of values.
Range can include numbers, date or text.
Syntax:
SELECT <fieldname(s)>
FROM <table_name>
WHERE <fieldname> Between <value1> and <value2>;
Note: both value1 and value2 are inclusive.
Example:
1. To display admno and names of students having marks between 40 to 70
Note: In the above Query both 40 and 70 are included in the output.
ORDER BY
Example:
1. To display the records of students in sorted order of their marks from lowest to
highest.
DISTINCT Clause
The DISTINCT keyword is used to remove duplicate values from a particular
column.
Syntax:
SELECT DISTINCT <column>
FROM <table_name>;
Example:
1. To Display unique classes from student table.
2. Display different sections from student table.
UPDATE Command
Note: Update command make changes in the original table so it should be used
very wisely. If ‘where clause’ is omitted ,it means all the records in the table will be
updated.
Example:
1. To Increase the marks of every student by 5.
Since there is no where clause all records have been updated. Let’s view the table
now to see the changes.
Syntax:
ALTER TABLE <table name>
[ADD/MODIFY/DROP] <column name>;
For example:
Note: A new column grade has been added in the table student.
DELETE Command
This command is used to remove the records from specified table.
Note: Delete command cannot be used to remove a column or a structure
of table.
Syntax:
Example:
Output:
Suman’s record is deleted from the table.
2. Remove the records of students who are born after 2005-01-01.’
Syntax:
For example:
Remove the whole structure of student table.
Aggregate functions
Aggregate function works on multiple rows and returns a single value. These
functions are also known as group functions. They are used to perform calculations
based upon a particular column.
Syntax:
SELECT aggregate_function(fieldname) from <table_name>;
Some examples of Aggregate functions are:
1. SUM()
2. AVG()
3. MAX()r Science
4. MIN()
5. COUNT()
SUM()
Example:
AVG()
MAX()
This function is used to find the maximum/highest value from a particular column.
Example:
MIN()
This function is used to find the minimum/Lowest value from a particular column.
Example:
COUNT()
This function is used to return a count of the non- NULL values in the particular column.
Example:
COUNT(*)
This function is used to find the number of values (i.e. number of rows) in a
particular column. It includes NULL values also.
Example:
GROUP BY
For example:
Since group by clause is used in the above query,we get three classes (6,7,8) in
the output.
Having clause
As mentioned earlier, the 'where' clause is used only to place condition on the
selected columns, whereas the 'HAVING' clause is used to place condition on
groups created by 'group by' clause, because here the 'WHERE' clause is not
useable.
Example:
1. Display the total strength of students in each class having strength less than
150.
In the above output, class 6 is not displayed as its total strength is more then 150.
Points to Remember
➢ Date: DATE
➢ String, char and date values are always enclosed within single quotes.