SQL12 (1)
SQL12 (1)
SQL provides wide range of effective command to perform all sort of required
operations on data such as create tables, insert record, view recodes, update,
alter, delete, drop, etc.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
What is DDL and DML?
All the SQL commands are categorized into five categories: DDL,DML,DCL,DQL,TCL. In this
course we are going to cover only DDL and DML commands in detail.
Data definition Language(DDL): Data Definition Language actually consists of the SQL
commands that can be used to define the database schema. It simply deals with
descriptions of the database schema and is used to create and modify the structure of
database objects in the database. Example: Create, Drop, Alter, Truncate.
Data Manipulation Language(DML): The SQL commands that deals with the manipulation of
data present in the database belong to DML or Data Manipulation Language and this
includes most of the SQL statements. Example: Insert, Delete, Update.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Data Types in MySQL
Data stored in a database table are of different types, As SQL developers we have chose the suitable data
types for each field while defining a table. SQL offers supports a wide range of data types from which the
developer can choose the most appropriate data types for each column.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
CREATE Database: Used to create a new database.
Syntax: CREATE DATABASE <database name>
e.g. CREATE Database MySchool;
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
Show Tables: After a database has been selected this command can be Used to list all the
tables in the database. e.g. SHOW TABLES;
CREATE Table: Syntax: CREATE TABLE <table name>( column1 datatype, column2 datatype,
column3 datatype, .... columnN datatype,
PRIMARY KEY( one or more columns ) );
E.g. CREATE TABLE cs_students(sid int(3), sname varchar(30), sclass int(2), smark int(3), skill
varchar(30), primary key(sid));
Note: Constraints other then Primary Key can also be specified when required
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
Creating a table with multiple constraints:
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
ALTER Tables: ALTER TABLE is a DDL command that can change the structure of the table.
Using ALTER TABLE command we can add, delete or modify the attributes/constraints of a
table.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
Modify a column using Alter table:
Syntax: ALTER TABLE <table name> MODIFY column <Column Name Data type>;
E.g. MODIFY the data type of an existing column
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
DROP Tables: DROP TABLE is a DDL command used to delete a table from the database.
Syntax: DROP TABLE <table name>;
E.g. DROP Table Employee;
INSERT INTO: INSERT is a DML command used to insert a new record/row in an existing
table.
Syntax: INSERT INTO <Table Name> values (val1,val2,val3..);
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
SELECT Command: Used to retrieve and Table: cs_students
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
MySql Commands:
WHERE Clause: The WHERE Clause can be used with SELECT command to select the data
from the table based on some condition.
Syntax: SELECT column1, column2,.. E.g. Select * From cs_students WHERE smark>90;
FROM <Table Name>
WHERE <condition>;
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Where Clause Examples:
To select ID and Name of the students whose skill is Database:
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
IN Operator: Used To specify multiple possible values for a column
E.g. Select * from cs_student where skill in(“Networking”, ”Database”);
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
DISTINCT Clause: Used to retrieve the distinct values in a field.
Syntax: Select * from student where mark is null;
ORDER BY: It is used to sort the data in ascending or descending order. By default ORDER BY sort the
data in ascending order, for descending order we need to use ”DESC”.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Handling NULL Values: To handle NULL entries in a field we can use “IS” and “IS NOT”, as NULL value
is a Value which is Unknown so we can use =, <> operators to select NULL values.
Lets Consider the Employee table above, to select all the employees whose salary is specified as
NULL in the salary field we must use IS NULL operator.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
LIKE OPERATOR: LIKE is used for string matching in MySql, it can be used for comparison of
character strings using pattern. LIKE uses the following two wildcard characters to create string
patterns.
• Percent(%): used to match a substring of any length.
• Underscore( _ ): Used to match any single character.
The LIKE keyword selects the rows having column values that matches with the wildcard pattern.
e.g. To select details of employees whose name start with the letter “S”.
To select details of employees whose name ends with the letter “l” and has exactly a 5 characters name.
Note: Patterns are case sensitive, upper case characters do not match with lower case characters or vice-versa.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Update Command : UPDATE is a DML command used to change values in the rows of a existing
table. It specifies the rows to be changed using WHERE clause and the new values to be updated
using SET keyword.
Syntax: UPDATE <Table Name> SET column=<new value> WHERE <condition>
E.g. To change the salary to 70000 of the employee having Eid 204.
UPDATE employee SET salary=70000 WHERE Eid=204.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Aggregate Functions :
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Aggregate Functions Example:
Let us Consider the employee table:
Queries Output
SELECT sum(salary) FROM employee; 80000
SELECT avg(salary) FROM employee; 26666.6666
SELECT max(salary) FROM employee; 32000
SELECT min(salary) FROM employee; 23000
SELECT count(salary) FROM employee; 3
SELECT count(*) FROM employee; 5
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
GROUP BY:
Table: cs_students
• GROUP BY clause combines all those records
that have identical values in a particular field or
a group of fields.
• It is used in SELECT statement to divide the
table into groups. Grouping can be done by a
column name or with aggregate functions.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
HAVING Clause: Table: cs_students
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
HAVING Examples:
Conditions in having clause may contain a Boolean
expression or aggregate functions can be used to specify the Table: Employee
condition.
We can also use more then one condition in HAVING clause
by using logical operators.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
JOIN:
A JOIN clause combines rows from two or more tables. In a join query, more
then one table are listed in FORM clause.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Cartesian Product (X):
The Cartesian Product operation of two tables produces all possible concatenations of all
the rows of both tables.
The Cartesian product(also known as Cross Join) multiplies all rows present in the first
table with all the rows present in the second table
Table 1 Table 2
Syntax: SELECT * FROM Table1,Table2; 1
1
Or 2
2
3
SELECT * FROM Table1 CROSS JOIN Table2;
Table: Orders
To perform Equi/Inner Join on two relations R1 and R2, we have to specify a equality condition using
the common attribute in both the relations R1 and R2. Syntax: SELECT * FROM R1 Inner Join R2.
Table: Customer Table: Orders
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Natural Join :
The Join in which only one of the identical columns(coming from joined tables) exists, is called as
Natural Join.
The Equi Join and Natural join are equivalent except that duplicate columns are eliminated in the
Natural Join that would otherwise appear in Equi Join. Table: Orders
Syntax: SELECT * FROM Table1 Natural Join Table2
Performing natural Join on Customer and Orders Table, results
in selecting only those rows from both the tables which have
matching values in common attribute CustId.
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Examples on Join :
Table: Customer Table: Orders
To display the Customer Id, Customer Name and order Quantity of those Customers having minimum
order quantity of 20.
Solution Using Cartesian Product:
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE
Examples on Join :
Table: Customer Table: Orders
To display the OrderID along with the customer name and city of the customers having order quantity
less then 20.
Solution Using Cartesian Product:
KV Coders
LET’S CRACK CBSE COMUPTER SCIENCE