Practical guide for a beginner who wants to learn SQL, which is a quite important language for working with any RDBMS (Relational Database Management System) like Oracle, MySql, DB2 etc..
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
Sql (Introduction to Structured Query language)
1. CREATE DATABASE database_name;
Then create connection with database. The connection is included in the names of the
database.
Create a Table: the following query creates a table
CREATE TABLE table_name
(
column_name1 data_type,
.......
)
CREATE TABLE email
(
LName varchar(20),
FName varchar(20),
email_add varchar(40)
)
INSERT INTO 'table_name'('field_name', 'field_name'. . .)
VALUES ('field_value', 'field_value'. . .);
INSERT INTO employee
VALUES ('Amar', 'Designer', 10000, 'amar@roseindia.com');
The output of the above code will be:
employee:
emp_name Position Salary email_id
Amar Designer 10000 amar@roseindia.com
Insert Data in Specified Columns
Let us consider we have a table named employee which have the following records:
emp_name Position Salary email_id
Amar Designer 10000 amar@roseindia.com
Let us consider we want to insert data in field name 'emp_name', 'Position' and in 'email_id'
with there specific values then we should use the following SQL statement:
INSERT INTO employee (emp_name, Position, email_id)
VALUES ('Vinod', 'Programmer', 'vinod@roseindia.com');
The output is like:
emp_name Position Salary email_id
Amar Designer 10000 amar@roseindia.com
Vinod Programmer vinod@roseindia.com
emp_name Position Salary email_id
Amar Designer 8000 amar@roseindia.com
If we want to change the salary to the employee with a emp_name of "Amar" then we should use the
following SQL statement :
Syntax:
UPDATE 'table_name' SET 'field_name' = 'new_value'
WHERE 'field_name' = 'field_value';
for example:
UPDATE Person SET Salary = 10000
WHERE emp_name = 'Amar';
The output of the above code will be :
emp_name Position Salary email_id
Amar Designer 10000 amar@roseindia.com
To Update several Columns in a Row: If we want to change the multiple values of a table like in employee
table we want to change Position and email_id then we have to write the following code in which we set
2. the email_id and position by SET keyword and putting condition by keyword WHERE for emp_name is
'Amar'.
UPDATE employee
SET email_id = 'amar@newsindia.com', Position = 'Programmer'
WHERE emp_name = 'Amar';
The output of the above code will be:
emp_name Position Salary email_id
Amar Programmer 10000 amar@newsindia.com
DELETE FROM table_name
WHERE column_name = some_value
Let's consider a table :
Person:
LName FName Address
ram Raj delhi-5
sonu Shiv delhi-5
To Delete a Row :
Suppose the row with Lname="sonu" is needed to get deleted
DELETE FROM Person WHERE LName = 'sonu'
Result
LName FName Address
ram Raj delhi-5
To Delete All the Rows : This means that the table structure, attributes, and indexes will be drop.
DELETE FROM table_name
SELECT column_name(s)
FROM table_name
Example: To select the content of columns named "LName" and "FName", from the database table called
"email", use a SELECT .
SELECT Name, Father_Name FROM EMAIL
The database table "email":
Name Father_Name Address
Ram Sonu delhi-5
Select All Columns: To select all columns from the "email" table, use a * symbol instead of column
names.
SELECT * FROM email
SELECT column FROM table
WHERE column operator value
The following operators can be used:
Operator Description
= Equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to return for at
least one of the columns
WHERE in SQL:
we add a WHERE to the SELECT statement:
SELECT * FROM Persons
WHERE unit='india'
In this section we are going to illustrate aggregate function, with the help of which we can use many
arithmetic operation like average, count, maximum and many more in SQL. They are briefly described and
denoted by the keyword in the given below section.
3. AVG
COUNT
MAX
MIN
SUM
For all of the above given function the standard code syntax will be:
SELECT "function type" ("column_name") FROM
"table_name"
For example we just take a Employee table and use the aggregate function SUM on the field "Salary" to
find out the total salary of the Employee table.
Table Employee:
emp_Name Salery Joining_Date
Amit 15000 Feb-05-2005
Chandan 25000 Feb-17-2005
Ravi 8000 Feb-25-2005
Vinod 7000 Feb-28-2005
To find out the total salary of the company's employee we should write the following code:
SELECT SUM (Salary) FROM Employee;
When we run the above query we will get the following result:
SUM(Salary)
55000
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
SQL % Wildcards Syntax
SELECT ColumnName(s)
FROM TableName
WHERE ColumnName LIKE pattern
SQL % Wildcards Query
Now we want to select the persons name those name starts with "k" from the table above.
select * from Stu_Table
where Stu_Name like 'k%'
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
1 Komal 10
Now we want to select the persons name those name ends with "h" from the table above.
select * from Stu_Table
where Stu_Name like '%h'
Result
Stu_Id Stu_Name Stu_Class
3 Rakesh 10
4. 5 Santosh 10
reate Table Stu_Class_10
create table Stu_Class_10(
Stu_Id integer(2) NOT NULL,
Stu_Name varchar(15),
Stu_Class varchar(10),
UNIQUE (id))
Insert data into Stu_Class_10
insert into Stu_Class_10 values(1,'Komal',10);
insert into Stu_Class_10 values(2,'Ajay',10);
insert into Stu_Class_10 values(3,'Rakesh',10);
insert into Stu_Class_10 values(4,'Bhanu',10);
insert into Stu_Class_10 values(5,'Santosh',10);
Stu_Class_10
Stu_Class_10
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Create Table Stu_Class_10
create table Stu_Class_10(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Create Table Stu_Class_12
create table Stu_Class_12(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Class_10
insert into Stu_Class_10 values(1,'Komal',10)
insert into Stu_Class_10 values(2,'Ajay',10)
insert into Stu_Class_10 values(3,'Rakesh',10)
insert into Stu_Class_10 values(4,'Bhanu',10)
insert into Stu_Class_10 values(5,'Santosh',10)
insert into Stu_Class_10 values(1,'Komal',10)
Insert data into Stu_Class_12
insert into Stu_Class_12 values(1,'Komal',12)
insert into Stu_Class_12 values(1,'Komal',12)
insert into Stu_Class_12 values(2,'Ajay',12)
insert into Stu_Class_12 values(3,'Rakesh',12)
insert into Stu_Class_12 values(4,'Bhanu',12)
insert into Stu_Class_12 values(5,'Santosh',12)
Stu_Class_10
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
Stu_Class_12
Stu_Id Stu_Name Stu_Class
1 Komal 12
1 Komal 12
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12
SQL UNION Syntax
The SQL UNION Syntax used for union columns from two tables is given below:
SELECT column_name(s) FROM table_name1
UNION All
SELECT column_name(s) FROM table_name2
Use UNION in SQL Query
5. In this example, we union columns from two different tables. The UNION ALL combine two table using
select statement when both the table have the same name field and its data type. The select return you
all duplicate records from both tables. The UNION ALL command select all records from a tables.
.SELECT * FROM Stu_Class_10
UNION ALL
SELECT * FROM Stu_Class_12
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
1 Komal 12
1 Komal 12
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12
Create Table Stu_Class_10
create table Stu_Class_10(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Create Table Stu_Class_12
create table Stu_Class_12(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Class_10
insert into Stu_Class_10 values(1,'Komal',10)
insert into Stu_Class_10 values(2,'Ajay',10)
insert into Stu_Class_10 values(3,'Rakesh',10)
insert into Stu_Class_10 values(4,'Bhanu',10)
insert into Stu_Class_10 values(5,'Santosh',10)
Insert data into Stu_Class_12
insert into Stu_Class_12 values(1,'Komal',12)
insert into Stu_Class_12 values(1,'Komal',12)
insert into Stu_Class_12 values(2,'Ajay',12)
insert into Stu_Class_12 values(3,'Rakesh',12)
insert into Stu_Class_12 values(4,'Bhanu',12)
insert into Stu_Class_12 values(5,'Santosh',12)
Stu_Class_10
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Stu_Class_12
Stu_Id Stu_Name Stu_Class
1 Komal 12
1 Komal 12
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12
SQL UNION Syntax
The Syntax used for SQL UNION is used to selects only distinct values by default. The Syntax used for SQL
UNION is given below:.
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
Use UNION in SQL Query
In this example, we make use of UNION in SQL Query, The UNION query return you the set of distinct
records from both the tables. The UNION operator only works with select statement, when both the table
6. have same field name and data type. The given below Query return you the distinct value from both the
tables.
SELECT * FROM Stu_Class_10
UNION
SELECT * FROM Stu_Class_12
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 12
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10));
Create Table Lib_Table
create table Lib_Table(Stu_Id integer(2), Lib_no integer(5));
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(8,'Tanuj',10);
Insert data into Lib_Table
insert into Lib_Table values(1,101);
insert into Lib_Table values(2,102);
insert into Lib_Table values(3,103);
insert into Lib_Table values(4,104);
insert into Lib_Table values(5,105);
insert into Lib_Table values(6,106);
insert into Lib_Table values(7,107);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
8 Tanuj 10
Lib_Table
Stu_Id Lib_no
1 101
2 102
3 103
4 104
5 105
6 106
7 107
SQL RIGHT JOIN Syntax
The SQL RIGHT JOIN Syntax display you all the records from the right table, even there are no matches
with the left table. The Syntax used for SQL RIGHT OUTER JOIN is given below:
SELECT column_name(s)
FROM table_name1
right JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Use Right JOIN in SQL Query
7. The Example shows you the RIGHT JOIN in SQL Query. In this Example, we use RIGHT OUTER JOIN,
which displays the records from two tables. The table contain return all record from table1,even there are
no matches with the table 2.
select s.stu_id, s.stu_name, s.stu_class, l.lib_no
from stu_table as s Right JOIN lib_table as l
on l.stu_id = s.stu_id
Result
Stu_Id Stu_Name Stu_Class Lib_No
1 Komal 10 101
2 Ajay 10 102
3 Rakesh 10 103
4 Bhanu 10 104
5 Santosh 10 105
NULL NULL NULL 106
NULL NULL NULL 107
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
The insert into add the records or rows value to the table 'Stu_Table'.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
SQL IN Syntax
The SQL IN Syntax is used to return the records from table specify multiple values in WHERE Clause.
SELECT ColumnName(s)
FROM TableName
WHERE ColumnName IN(value1, value2,...)
SQL IN Query
The given below Query return you the records from table stu_table specifying multiple value in Stu_Name
containing name 'komal' and 'Rakesh'.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
WHERE Stu_Name IN('Komal', 'Rakesh')
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
3 Rakesh 10
1 Komal 10
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15),
Stu_Class varchar(10), Stu_Dob Date)
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10,'1999,11,1');
insert into Stu_Table values(2,'Ajay',10,'1999,10,1');
insert into Stu_Table values(3,'Rakesh',10,'1999,11,1');
insert into Stu_Table values(4,'Bhanu',10,'1999,11,1');
insert into Stu_Table values(5,'Santosh',10,'1999,11,1');
Stu_Table
Stu_Id Stu_Name Stu_Class Stu_Dob
8. 1 Komal 10 1999-11-1
2 Ajay 10 1999-10-1
3 Rakesh 11 1999-11-1
4 Bhanu 11 1999-11-1
5 Santosh 12 1999-11-1
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
SQL LIKE Syntax
The SQL BETWEEN operator helps you to return a records between two values. The Syntax used in SQL
Like is given below:
SELECT ColumnName(s)
FROM TableName
WHERE ColumnName value1 between value2
SQL Like Operator Query
The SQL Like Operator Query is used to select the Stu_Table where a stu_Id between 1 and 3 from the
table.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
WHERE Stu_Id BETWEEN 1 and 3
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
1 Komal 10
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',11);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 11
SQL AND Operator Syntax
The SQL AND Operator Syntax return you a filter records or rows from a table based on condition. The
Syntax used for SQL AND Operator is given as :
SELECT columnName(s)
9. FROM tableName
where condition1 AND conditions2
SQL AND Operator Query
The given SQL Query uses SQL AND Operator to fetch the record from table 'Stu_Table' based upon where
clause, which restricts the records from table based upon the AND operator condition. The AND operator
query return you a record only ,if the both condition specified in query are true. Otherwise, no records
fetch from a table.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
where Stu_Id = 1 AND Stu_name = 'komal'
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
1 Komal 11
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10)
insert into Stu_Table values(2,'Ajay',12)
insert into Stu_Table values(3,'Rakesh',12)
insert into Stu_Table values(4,'Bhanu',12)
insert into Stu_Table values(5,'Santosh',12)
insert into Stu_Table values(1,'Komal',10)
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 12
3 Rakesh 12
4 Bhanu 12
5 Santosh 12
1 Komal 10
SQL AND Operator Syntax
The SQL AND Operator Syntax return you the filter record from a table based upon the condition specified
in the query.
SELECT columnName(s)
FROM tableName
where condition1 AND conditions2
SQL AND Operator Query
The given SQL Query help you to select only the Stu_Table with the Stu_Id equal to"1" AND
Stu_class="10".The AND operator fetch the records from a table, if both the condition are to be true. In
case, any one of the condition is false, fetch no records from table.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
where Stu_Id = 1 AND Stu_Class = 10
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
1 Komal 10
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10)
insert into Stu_Table values(2,'Ajay',10)
insert into Stu_Table values(3,'Rakesh',10)
insert into Stu_Table values(4,'Bhanu',10)
insert into Stu_Table values(5,'Santosh',10)
insert into Stu_Table values(1,'Komal',10)
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
10. 4 Bhanu 10
5 Santosh 10
1 Komal 10
Syntax
The below Syntax helps you to fetch the records from the table. The Like query is used to find and
search for a specified records from a table in database..
SELECT ColumnName(s)
FROM TableName
WHERE ColumnName LIKE pattern
Query
The given below Query fetch the record from table 'Stu_Table using WHERE clause, which restrict the
records based upon the Like Query, that is used to find and search for a specific records from a table
Stu_Table.
The % (wildcard) is used to specify wildcards, which indicates missing letters in the pattern before and
after the pattern.
In this code, we want to select the Stu_Table begin with a stu_name "k" whose id is"1".The AND operator
is used to satisfy the both condition. If the both condition is true returns you a record from table. The
record display a Stu_Name begin with "k" whose id is "1".
select * from Stu_Table
where stu_name like 'k%'AND stu_id = 1
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
1 Komal 10
1. Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
SQL Alter Query
Alter table Stu_Table rename to Stu_Table_10)
2.
Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(10),
Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');
Stu_Table
+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
11. | 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
+--------+----------+-----------+
Describe Stu_Table
The Describe Stu_Table show you the field, data type, null etc of the table 'Stu_Table'.
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | YES | | | |
| Stu_Name | varchar(10) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+-------------+------+-----+---------+-------+
Alter column type Query
The Alter Table alter the table 'stu_Table' and MODIFY keywords modify the data type of Stu_Id(varchar
(2)) into Stu_Id( int(3)).
ALTER TABLE Stu_Table MODIFY Stu_Id int(3)
Describe Stu_Table
When you see the structure of table using Describe Stu_Table, the output is displayed as:
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Stu_Id | int(3) | YES | | | |
| Stu_Name | varchar(10) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+--------------+------+-----+---------+-------+
Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');
Stu_Table
+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
+--------+----------+-----------+
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | YES | | | |
| Stu_Name | varchar(15) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+-------------+------+-----+---------+-------+
Alter Column Size Query
ALTER TABLE Stu_Table MODIFY Stu_Id varchar(100)
Describe Stu_Table
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Stu_Id | varchar(100) | YES | | | |
| Stu_Name | varchar(15) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+--------------+------+-----+---------+-------+
12. Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(10),
Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');
Stu_Table
+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
+--------+----------+-----------+
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | YES | | | |
| Stu_Name | varchar(10) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+-------------+------+-----+---------+-------+
Alter column Not Null Query
ALTER TABLE Stu_Table MODIFY Stu_Id int(3)not null
Describe Stu_Table
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Stu_Id | int(3) | No | | | |
| Stu_Name | varchar(10) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+--------------+------+-----+---------+-------+
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15),
Stu_Class varchar(10) )
Insert data into Stu_Table
insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');
Stu_Table
+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
+--------+----------+-----------+
Syntax
The ALTER table statement in SQL is used to modify the table 'Stu_Table' and change keyword change
the name of field to new name of field. The syntax used for Alter Table is given below:
Alter table table_name change old_column_name new_column_name type size
Query
The Alter Table alter the table 'Stu_Table'. The change keyword change the column name of Stu_Id to Id
in table 'Stu_Table'.
Alter table Stu_Table change Stu_Id Id varchar(10)
13. Stu_Table
+--------+----------+-----------+
| Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
+--------+----------+-----------+
Create Table Stu_Table
create table Stu_Table( Stu_Id integer(2), Stu_Name varchar(15),
Stu_Class varchar(10))
Insert data into Stu_Table
insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');
Stu_Table
+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
+--------+----------+-----------+
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(10) | YES | | | |
| Stu_Name | varchar(10) | YES | | NULL | |
| Stu_Class | varchar(5) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
Alter column Column Default Query
The Alter Table Keywords modify table'Stu_Table' and MODIFY keyword modify the data type of field
Stu_Id (varchar(10)) to Stu_Id(int(3)) and set the default value for this field is set '10'.Whenever you
leave a blank value in Stu_Id, This field would take default value of '10'.
ALTER TABLE Stu_Table MODIFY Stu_Id int(3) Default '10'
Describe Stu_Table
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Stu_Id | int(3) | No | | 10 | |
| Stu_Name | varchar(10) | YES | | NULL | |
| Stu_Class | varchar(5) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
The insert into insert the records or row values into table 'Stu_Table'.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(6,'Tanuj',10);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
14. 3 Rakesh 10
4 Bhanu 10
5 Santosh 10
6 Tanuj 10
SQL ALTER Syntax
The SQL ALTER Syntax is used to modify the table and add a column name that you want to add and its
data type.
ALTER TABLE table_name
ADD column_name column-definition
SQL ALTER Query
The given below Query alter a table 'Stu_Table' and add keyword add a column Stu_Class whose data
type is varchar type.
Alter Table Stu_Table add Stu_Class varchar(10)
View data Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal NULL
2 Ajay NULL
3 Rakesh NULL
4 Bhanu NULL
5 Santosh NULL
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Create Table Lib_Table
create table Lib_Table(Stu_Id integer(2), Lib_no integer(5))
Insert data into Stu_Table
The insert into add the records or rows to the table 'Stu_Table' and 'Lib_Table'.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
Insert data into Lib_Table
insert into Lib_Table values(1,101)
insert into Lib_Table values(2,102)
insert into Lib_Table values(3,103)
insert into Lib_Table values(4,104)
insert into Lib_Table values(5,105)
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Lib_Table
Stu_Id Lib_no
1 101
2 102
3 103
4 104
5 105
SQL Alias Query
The given below SQL Query takes alias name for table 'Stu_Table' as s and 'Lib_Table' as l.
select s.stu_id, s.stu_name, s.stu_class, l.lib_no
from Stu_Table as s , Lib_Table as l
where s.stu_id = l.stu_id
Result
Stu_Id Stu_Name Stu_Class Lib_no
1 Komal 10 101
2 Ajay 10 102
15. 3 Rakesh 11 103
4 Bhanu 11 104
5 Santosh 12 105
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal')
insert into Stu_Table values(2,'Ajay')
insert into Stu_Table values(3,'Rakesh')
insert into Stu_Table values(4,'Bhanu')
insert into Stu_Table values(5,'Santosh)
Stu_Table
Stu_Id Stu_Name
1 Komal
2 Ajay
3 Rakesh
4 Bhanu
5 Santosh
SQL Add Column Syntax
The ALTER Table is used to modify table name 'table_name' and add a column at the specific position. The
first column specify the position of the column to be come first in table followed by a after column in a
table. The Syntax is given as :
ALTER TABLE table_name
ADD column_name column-definition [ FIRST | AFTER col_name ]
SQL Add Colum Query
The SQL Add Column Query add a Stu_Name column followed by Stu_Name in the Table Stu_Table.
Alter Table Stu_Table add Stu_Class int(10) AFTER Stu_Name
View data Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal NULL
2 Ajay NULL
3 Rakesh NULL
4 Bhanu NULL
5 Santosh NULL
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15))
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal');
insert into Stu_Table values(2,'Ajay');
insert into Stu_Table values(3,'Rakesh');
insert into Stu_Table values(4,'Bhanu');
insert into Stu_Table values(5,'Santosh);
Stu_Table
Stu_Id Stu_Name
1 Komal
2 Ajay
3 Rakesh
4 Bhanu
5 Santosh
SQL Add Column Syntax
The ALTER TABLE is used to alter the table table_name.The Add keyword is used to add a new column in a
table.
ALTER TABLE table_name
ADD column_name column-definition
SQL Add Colum Query
The given below example shows you a modified Stu_Table using Alter keywords, which add a new column
Stu_Class using add keyword, whose data is integer type.
Alter Table Stu_Table add Stu_Class int(10)
View data Stu_Table
16. Stu_Id Stu_Name Stu_Class
1 Komal NULL
2 Ajay NULL
3 Rakesh NULL
4 Bhanu NULL
5 Santosh NULL
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15),
Stu_Class varchar(10) default '10')
Insert data into Stu_Table
insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15))
Insert data into Stu_Table
The insert into add the records or rows into the table 'Stu_Table'.
insert into Stu_Table values(1,'Komal');
insert into Stu_Table values(2,'Ajay');
insert into Stu_Table values(3,'Rakesh');
insert into Stu_Table values(4,'Bhanu');
insert into Stu_Table values(5,'Santosh);
Stu_Table
Stu_Id Stu_Name
1 Komal
2 Ajay
3 Rakesh
4 Bhanu
5 Santosh
SQL Add Column Syntax
The Alter Table modifies and ADD Keywords add a new column to the table. The Syntax for the Add
Column is given as below:
ALTER TABLE table_name
ADD column_name column-definition
SQL Add Colum Query
The given below Query define a Alter Table that modifies and add a new column 'Stu_Class' to the table
'Stu_Table'.
Alter Table Stu_Table add Stu_Class varchar(10)
View data Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal NULL
2 Ajay NULL
3 Rakesh NULL
4 Bhanu NULL
5 Santosh NULL
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10));
Insert data into Stu_Table
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
17. insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
SQL LIKE Syntax
SELECT ColumnName(s)
FROM TableName
WHERE ColumnName LIKE pattern
SQL Like Operator Query
Now we want to select the persons name those name starts with "k" from the table above.
select * from Stu_Table
where stu_name like 'k%'
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
1 Komal 10
Now we want to select the persons name those name ends with "h" from the table above.
select * from Stu_Table
where stu_name like '%h'
Result
Stu_Id Stu_Name Stu_Class
3 Rakesh 10
5 Santosh 10
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10));
Insert data into Stu_Table
The Insert into statement add the records or rows into table 'Stu_Table'.
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(3,'Bhanu',10);
insert into Stu_Table values(4,'Santosh',10);
Stu_Table
The Select statement return you the records from a table 'Stu_Table'.
Records in the table:
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
SQL SELECT DISTINCT Syntax
The SQL Distinct clause can be used with the Select statement to get all unique records from database
SELECT DISTINCT columnName(s)
FROM tableName
SQL SELECT DISTINCT Query
You can run the following query to retrieve unique records from the table:
SELECT DISTINCT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table;
Result of the above query:
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
18. 3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Create Table Lib_Table
create table Lib_Table(Stu_Id integer(2), Lib_no integer(5))
Insert data into Stu_Table
The insert into add the records or rows to the respective tables.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(6,'Tanuj',10);
Insert data into Lib_Table
insert into Lib_Table values(1,101)
insert into Lib_Table values(2,102)
insert into Lib_Table values(3,103)
insert into Lib_Table values(4,104)
insert into Lib_Table values(5,105)
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
6 Tanuj 10
Lib_Table
Stu_Id Lib_no
1 101
2 102
3 103
4 104
5 105
SQL INNER JOIN Syntax
The INNER JOIN keyword is used to returns all rows from the left table (table_name1), even if there are
no matches in the right table (table_name2).The syntax for SQL INNER JOIN is given as :
SELECT column_name(s)
FROM table_name1
Left JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Use INNER JOIN in SQL Query
In this example, the inner join is used to return the records from a tables 'Stu_Table'and 'Lib_Table' using
select statement. The select statement include the set of column records to be retrieved from both tables.
The Left Join is used to return all rows from the left table ,even if there is no matches in the right table.
select s.stu_id, s.stu_name, s.stu_class, l.lib_no
from stu_table as s Left JOIN lib_table as l
on l.stu_id = s.stu_id
Ruselt
Stu_Id Stu_Name Stu_Class Lib_No
1 Komal 10 101
2 Ajay 10 102
3 Rakesh 10 103
4 Bhanu 10 104
5 Santosh 10 105
6 Tanuj 10 NULL
Create Table Stu_Class_10
CREATE TABLE Stu_Class_10(
Stu_Id integer(2) NOT NULL,
19. Stu_Name varchar(15) NOT NULL,
Stu_Class varchar(10)) NOT NULL)
Insert data into Stu_Class_10 table
insert into Stu_Class_10 values(1,'Komal',10);
insert into Stu_Class_10 values(2,'Ajay',10);
insert into Stu_Class_10 values(3,'Rakesh',10);
insert into Stu_Class_10 values(4,'Bhanu',10);
insert into Stu_Class_10 values(5,'Santosh',10);
Stu_Class_10
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
Insert into statement insert the records into table.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
Stu_Table
The select statement return you the record from table 'Stu_Table'.
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
1 Komal 10
SQL ORDER BY Syntax
The ORDER BY keyword sort the table result set by a specified column.
SELECT ColumnName(s)
FROM TableName
ORDER BY ColumnName(s)
SQL ORDER BY Query
In this Example, we use Order By Query on table 'Stu_Table'.The Order by Query return you the sorted
records from the table in ascending order by Stu_Name.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
ORDER BY Stu_Name
Result
output of the above SQL Query will be
Stu_Id Stu_Name Stu_Class
2 Ajay 10
4 Bhanu 10
1 Komal 10
1 Komal 10
3 Rakesh 10
5 Santosh 10
SQL ORDER BY Query
In this example, we sort the records from a table in descending order by Stu_Id.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
ORDER BY Stu_Id DESC
20. Result
Stu_Id Stu_Name Stu_Class
5 Santosh 10
4 Bhanu 10
3 Rakesh 10
2 Ajay 10
1 Komal 10
1 Komal 10
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
The insert into add the records or rows to the table Stu_Table.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',11);
insert into Stu_Table values(4,'Bhanu',11);
insert into Stu_Table values(5,'Santosh',12);
insert into Stu_Table values(1,'Komal',10);
Stu_Table
The select statement help you to display the table detail.
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 11
4 Bhanu 11
5 Santosh 12
1 Komal 10
SQL OR Operator Syntax
The SQL OR Operator is used to return a record if either the first condition is true or the second condition
is true. The Syntax used for SQL OR Operator is given below:
SELECT columnName(s)
FROM tableName
where condition1 OR conditions2
SQL OR Operator Query
In this example, we make use of OR Operator, which return the records from a table on the basis of
condition in WHERE Clause. The OR Operator return you a set of records,if either of the condition are
true.
SELECT Stu_Id, Stu_Name, Stu_Class
FROM Stu_Table
where Stu_Id = 1 OR Stu_Class = 10
Result
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
1 Komal 11
Create Table Stu_Class_10
create table Stu_Class_10(
Stu_Id integer(2) NOT NULL,
Stu_Name varchar(15),
Stu_Class varchar(10),
PRIMARY KEY(Stu_Id))
Insert data into Stu_Class_10
The insert into statement is used to add the records or rows to the table 'Stu_Class_10'.
insert into Stu_Class_10 values(1,'Komal',10)
insert into Stu_Class_10 values(2,'Ajay',10)
insert into Stu_Class_10 values(3,'Rakesh',10)
insert into Stu_Class_10 values(4,'Bhanu',10)
insert into Stu_Class_10 values(5,'Santosh',10)
Stu_Class_10
Stu_Id Stu_Name Stu_Class
1 Komal 10
21. 2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10))
Create Table Lib_Table
create table Lib_Table(Stu_Id integer(2), Lib_no integer(5))
Insert data into Stu_Table
The insert into add the records or rows to the respective tables.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
Insert data into Lib_Table
insert into Lib_Table values(1,101);
insert into Lib_Table values(2,102);
insert into Lib_Table values(3,103);
insert into Lib_Table values(4,104);
insert into Lib_Table values(5,105);
insert into Lib_Table values(6,106);
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Lib_Table
Stu_Id Lib_no
1 101
2 102
3 103
4 104
5 105
6 106
SQL INNER JOIN Syntax
The INNER JOIN keyword in SQL is used to returns you a row, when there is at least one match in both
table used in SQL Query. The Syntax used for SQL INNER JOIN is used as :
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Use INNER JOIN in SQL Query
In this example, we make use of inner join. The inner join return you the records from both table ,when
there is atleast one match in both table respectively. The inner join is known as join. In this Query,both
the tables have stu_id as primary key, based on it, return all the record from the column enlisted in select
statement.
select s.stu_id, s.stu_name, s.stu_class, l.lib_no
from stu_table as s inner JOIN lib_table as l
on l.stu_id = s.stu_id
Ruselt
Stu_Id Stu_Name Stu_Class Lib_No
1 Komal 10 101
2 Ajay 10 102
3 Rakesh 10 103
4 Bhanu 10 104
5 Santosh 10 105
Create Table Stu_Table
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10));
Create Table Lib_Table
22. create table Lib_Table(Stu_Id integer(2), Lib_no integer(5));
Insert data into Stu_Table
The insert into add the records value into your respective tables.
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
Insert data into Lib_Table
insert into Lib_Table values(1,101);
insert into Lib_Table values(2,102);
insert into Lib_Table values(3,103);
insert into Lib_Table values(4,104);
insert into Lib_Table values(5,105)
Stu_Table
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
Lib_Table
Stu_Id Lib_no
1 101
2 102
3 103
4 104
5 105
SQL Alias Query
The given below example show you the SQL Alias Query. In this case, the alias name for Stu_Table is 's'
and Lib_Table is 'l'.As you can see the Alias Name Query make the table name simple and easier to write
and read.
select s.stu_id, s.stu_name, s.stu_class, l.lib_no
from Stu_Table as s , Lib_Table as l
where s.stu_id = l.stu_id
Result
Stu_Id Stu_Name Stu_Class Lib_no
1 Komal 10 101
2 Ajay 10 102
3 Rakesh 11 103
4 Bhanu 11 104
5 Santosh 12 105
Create Table Stu_Table
Create Table Stu_Table (Stu_Id varchar(2), Stu_Name varchar(15), Stu_Dob date);
Insert Data Into Stu_Table
The insert into statement add the records or rows into the table 'stu_table'.
insert into stu_table values('1', 'komal', '1984-10-27');
insert into stu_table values('2', 'ajay', '1985-04-19');
insert into stu_table values('3', 'santosh', '1986-11-16');
Stu_Table
+--------+----------+------------+
| Stu_Id | Stu_Name | Stu_Dob |
+--------+----------+------------+
| 1 | komal | 1984-10-27 |
| 2 | ajay | 1985-04-19 |
| 3 | santosh | 1986-11-16 |
+--------+----------+------------+
Query
The given below Query return you the record from a table 'Stu_Table'. The Where Query restrict select
Query and show you the record between ' '1984-01-01' And '1986-1-1'.
Select * From Stu_Table
23. Where Stu_Dob Between '1984-01-01' And '1986-1-1';
Result
+--------+----------+------------+
| Stu_Id | Stu_Name | Stu_Dob |
+--------+----------+------------+
| 1 | komal | 1984-10-27 |
| 2 | ajay | 1985-04-19 |
+--------+----------+------------+
create table statement create a table Stu_Table.
Create Table Stu_Table
SQL statement to create table :
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub_id varchar(2),marks varchar(3));
Insert Data into Stu_Table
The insert into statement add the records or rows to the table 'Stu_Table'.
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(3,'Rakesh',10,4,37);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(2,'Ajay',10,5,46);
insert into Stu_Table values(3,'Rakesh',10,5,63);
Stu_Table
Records in the table:
+--------+----------+-----------+--------+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
| 1 | Komal | 10 | 1 | 45 |
| 2 | Ajay | 10 | 1 | 56 |
| 3 | Rakesh | 10 | 1 | 67 |
| 1 | Komal | 10 | 2 | 47 |
| 2 | Ajay | 10 | 2 | 53 |
| 3 | Rakesh | 10 | 2 | 57 |
| 1 | Komal | 10 | 3 | 45 |
| 2 | Ajay | 10 | 3 | 56 |
| 3 | Rakesh | 10 | 3 | 67 |
| 1 | Komal | 10 | 4 | 65 |
| 2 | Ajay | 10 | 4 | 56 |
| 3 | Rakesh | 10 | 4 | 37 |
| 1 | Komal | 10 | 5 | 65 |
| 2 | Ajay | 10 | 5 | 46 |
| 3 | Rakesh | 10 | 5 | 63 |
+--------+----------+-----------+--------+-------+
Query
The given below syntax show you the average marks records of a numeric field in a table. The Group By
clause is used with the SQL aggregate functions and indicates the group where selected rows are placed.
select stu_id, stu_name,GROUP_CONCAT(marks) as marks,
sum(marks)as total ,avg(marks) as per
from stu_table group by stu_id;
Result
+--------+----------+----------------+-------+------+
| stu_id | stu_name | marks | total | per |
24. +--------+----------+----------------+-------+------+
| 1 | Komal | 45,65,47,65,45 | 267 | 53.4 |
| 2 | Ajay | 56,56,56,53,56 | 277 | 55.4 |
| 3 | Rakesh | 67,57,67,67,63 | 321 | 64.2 |
+--------+----------+----------------+-------+------+
SQL statement to create table:
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub_id varchar(2),marks varchar(3));
Insert Data into Stu_Table
The insert into add the records or rows to the created table 'Stu_Table'.
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(3,'Rakesh',10,5,63);
Stu_Table
Records in the table:
+--------+----------+-----------+--------+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
| 1 | Komal | 10 | 1 | 45 |
| 2 | Ajay | 10 | 1 | 56 |
| 3 | Rakesh | 10 | 1 | 67 |
| 1 | Komal | 10 | 2 | 47 |
| 2 | Ajay | 10 | 2 | 53 |
| 3 | Rakesh | 10 | 2 | 57 |
| 1 | Komal | 10 | 3 | 45 |
| 2 | Ajay | 10 | 3 | 56 |
| 3 | Rakesh | 10 | 3 | 67 |
| 1 | Komal | 10 | 4 | 65 |
| 2 | Ajay | 10 | 4 | 56 |
| 1 | Komal | 10 | 5 | 65 |
| 3 | Rakesh | 10 | 5 | 63 |
+--------+----------+-----------+--------+-------+
Query
The given below Query return you the records from table 'Stu_Table' and aggregate count for the table
field 'stu_name'. The Group By clause in this Query is used with aggregate functions and also specifies the
group 'stu_id' where selected rows are placed.
select stu_id, stu_name,
count(stu_name) from stu_table group by stu_id;
Result
+--------+----------+-----------------+
| stu_id | stu_name | count(stu_name) |
+--------+----------+-----------------+
| 1 | Komal | 5 |
| 2 | Ajay | 4 |
| 3 | Rakesh | 4 |
+--------+----------+-----------------+
Create Table Stu_Table
SQL statement to create table:
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
25. insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(6,'Tanuj',10);
Stu_Table
Records in the table:
Stu_Id Stu_Name Stu_Class
1 Komal 10
2 Ajay 10
3 Rakesh 10
4 Bhanu 10
5 Santosh 10
6 Tanuj 10
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | NO | | | |
| Stu_Name | varchar(15) | YES | | | |
| stu_class | varchar(10) | NO | | | |
+-----------+-------------+------+-----+---------+-------+
Alter Table Primary Key Syntax
The table_name is the name of the table on which primary key is added.
The column_name is the name of the column on which primary key is created.
ALTER TABLE table_name
ADD PRIMARY KEY (column_name)
Alter Table Primary Key Query
The given Query show you an example to alter a table name 'Stu_Table' on which primary key is added.
The 'Stu_Id' is the name of the column on which primary key is created.
ALTER TABLE Stu_Table
ADD PRIMARY KEY (Stu_Id)
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | NO | PRI | | |
| Stu_Name | varchar(15) | YES | | | |
| stu_class | varchar(10) | NO | | | |
+-----------+-------------+------+-----+---------+-------+
reate Table
SQL statement to create table:
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15), Stu_Class varchar(10));
Insert Data
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(6,'Tanuj',10);
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | YES | | | |
| Stu_Name | varchar(15) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
+-----------+-------------+------+-----+---------+-------+
Stu_Table
Records in the table:
+--------+----------
+-----------+
| Stu_Id | Stu_Name | Stu_Class |
26. +--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
| 6 | Tanuj | 10 |
+--------+----------+-----------+
Add Multiple Columns Query
The Alter Table Query in SQL modifies the existing table 'Stu_Table' and add a multiple columns to the
existing table. In this example, we add columns 'Stu_dob' and 'Stu_Address' to the existing table
'Stu_Table'.
Alter Table Stu_Table add (Stu_dob date, Stu_Addreass varchar(20));
Describe Stu_Table
The Describe Stu_Table show you the new fields added to the table'Stu_Table'.
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| Stu_Id | varchar(2) | YES | | | |
| Stu_Name | varchar(15) | YES | | | |
| Stu_Class | varchar(10) | YES | | | |
| Stu_dob | date | YES | | | |
| Stu_Addreass | varchar(20) | YES | | | |
+--------------+-------------+------+-----+---------+-------+
Stu_Table
+--------+----------+-----------+---------+--------------+
| Stu_Id | Stu_Name | Stu_Class | Stu_dob | Stu_Addreass |
+--------+----------+-----------+---------+--------------+
| 1 | Komal | 10 | | |
| 2 | Ajay | 10 | | |
| 3 | Rakesh | 10 | | |
| 4 | Bhanu | 10 | | |
| 5 | Santosh | 10 | | |
| 6 | Tanuj | 10 | | |
+--------+----------+-----------+---------+--------------+
Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub_id varchar(2),marks varchar(3));
Insert Data into Stu_Table
The insert into add the records or rows to the table'Stu_Table'.
insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(3,'Rakesh',10,4,37);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(2,'Ajay',10,5,46);
insert into Stu_Table values(3,'Rakesh',10,5,63);
Stu_Table
+--------+----------+-----------+--------+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
| 1 | Komal | 10 | 1 | 45 |
| 2 | Ajay | 10 | 1 | 56 |
| 3 | Rakesh | 10 | 1 | 67 |
| 1 | Komal | 10 | 2 | 47 |
| 2 | Ajay | 10 | 2 | 53 |
| 3 | Rakesh | 10 | 2 | 57 |
27. | 1 | Komal | 10 | 3 | 45 |
| 2 | Ajay | 10 | 3 | 56 |
| 3 | Rakesh | 10 | 3 | 67 |
| 1 | Komal | 10 | 4 | 65 |
| 2 | Ajay | 10 | 4 | 56 |
| 3 | Rakesh | 10 | 4 | 37 |
| 1 | Komal | 10 | 5 | 65 |
| 2 | Ajay | 10 | 5 | 46 |
| 3 | Rakesh | 10 | 5 | 63 |
+--------+----------+-----------+--------+-------+
Query
The given below Query display you the records from table 'Stu_Table'. The 'sum(marks)' in the select
statement compute the sum of marks. The Group By Clause in this query returns you the group of
result-set by column name'stu_id'.
select stu_id, stu_name, sum(marks) as 'total marks'
from stu_table group by stu_id;
Result
+--------+----------+-------------+
| stu_id | stu_name | total marks |
+--------+----------+-------------+
| 1 | Komal | 267 |
| 2 | Ajay | 267 |
| 3 | Rakesh | 291 |
+--------+----------+-------------+
Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub_id varchar(2),marks varchar(3));
Insert Data into Stu_Table
The Insert into statement add the records or rows to the table 'Stu_Table'.
insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(3,'Rakesh',10,4,37);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(2,'Ajay',10,5,46);
insert into Stu_Table values(3,'Rakesh',10,5,63);
Stu_Table
+--------+----------+-----------+--------
+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
| 1 | Komal | 10 | 1 | 45 |
| 2 | Ajay | 10 | 1 | 56 |
| 3 | Rakesh | 10 | 1 | 67 |
| 1 | Komal | 10 | 2 | 47 |
| 2 | Ajay | 10 | 2 | 53 |
| 3 | Rakesh | 10 | 2 | 57 |
| 1 | Komal | 10 | 3 | 45 |
| 2 | Ajay | 10 | 3 | 56 |
| 3 | Rakesh | 10 | 3 | 67 |
| 1 | Komal | 10 | 4 | 65 |
| 2 | Ajay | 10 | 4 | 56 |
| 3 | Rakesh | 10 | 4 | 37 |
| 1 | Komal | 10 | 5 | 65 |
| 2 | Ajay | 10 | 5 | 46 |
| 3 | Rakesh | 10 | 5 | 63 |
+--------+----------+-----------+--------+-------+
28. Query
The Query below returns you the records from select statement. The GROUPCONCAT is used to combine
the result value of field. The max(marks) compute the maximum value of the field 'marks'.
select stu_id, stu_name, GROUP_CONCAT(marks) as marks ,
max(marks) from stu_table group by stu_id
Result
+--------+----------+----------------+------------+
| stu_id | stu_name | marks | max(marks) |
+--------+----------+----------------+------------+
| 1 | Komal | 45,65,47,65,45 | 65 |
| 2 | Ajay | 46,56,56,53,56 | 56 |
| 3 | Rakesh | 67,57,37,67,63 | 67 |
+--------+----------+----------------+------------+
Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub1 integer(2),sub2 integer(2),
sub3 integer(2),sub4 integer(2),sub5 integer(2));
Insert Value Into Stu_Table
The insert into statement add the records or rows to the table.
insert into Stu_Table values(1, 'Komal', 10, 23, 34, 45, 56, 67);
insert into Stu_Table values(2, 'Ajay', 10, 34, 45, 56, 67, 78);
insert into Stu_Table values(3, 'Rakesh', 10, 23, 43, 45, 45, 34);
insert into Stu_Table values(4, 'Bhanu', 10, 56, 45, 67, 34, 54);
insert into Stu_Table values(5, 'Santosh', 10, 56, 67, 56, 67, 56);
insert into Stu_Table values(6, 'Tanuj', 10, 56, 45, 56, 56, 45);
Stu_Table
+--------+----------+-----------+------+------+------+------
+------+
| Stu_Id | Stu_Name | Stu_Class | sub1 | sub2 | sub3 | sub4 | sub5 |
+--------+----------+-----------+------+------+------+------+------+
| 1 | Komal | 10 | 23 | 34 | 45 | 56 | 67 |
| 2 | Ajay | 10 | 34 | 45 | 56 | 67 | 78 |
| 3 | Rakesh | 10 | 23 | 43 | 45 | 45 | 34 |
| 4 | Bhanu | 10 | 56 | 45 | 67 | 34 | 54 |
| 5 | Santosh | 10 | 56 | 67 | 56 | 67 | 56 |
| 6 | Tanuj | 10 | 56 | 45 | 56 | 56 | 45 |
+--------+----------+-----------+------+------+------+------+------+
Query
The given below Query returns you the records enlisted in select statement and sum of different numeric
field for each individual group of column values in the table 'Stu_Table'.
select stu_id, stu_name, sub1, sub2, sub3, sub4, sub5,
sum( sub1 + sub2 + sub3 + sub4 + sub5) as total
from stu_table group by stu_id;
Result
+--------+----------+------+------+------+------+------+-------+
| stu_id | stu_name | sub1 | sub2 | sub3 | sub4 | sub5 | total |
+--------+----------+------+------+------+------+------+-------+
| 1 | Komal | 23 | 34 | 45 | 56 | 67 | 225 |
| 2 | Ajay | 34 | 45 | 56 | 67 | 78 | 280 |
| 3 | Rakesh | 23 | 43 | 45 | 45 | 34 | 190 |
| 4 | Bhanu | 56 | 45 | 67 | 34 | 54 | 256 |
| 5 | Santosh | 56 | 67 | 56 | 67 | 56 | 302 |
| 6 | Tanuj | 56 | 45 | 56 | 56 | 45 | 258 |
+--------+----------+------+------+------+------+------+-------+
create the Table Stu_Table
SQL statement to create table:
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10));
Insert data into Stu_Table
Insert into keyword add the records or rows to the table 'Stu_Table'.
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
29. insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(6,'Tanuj',10);
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
Stu_Table
Records in the table:
+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
| 6 | Tanuj | 10 |
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
+--------+----------+-----------+
Query
The given Query return you the records and retrieve a single value, by calculate from values in a column
'stu_name' enlisted in the select statement from 'Stu_Table'. The group by keyword group all the records
which is grouped by attribute 'stu_id' from a table 'Stu_Table'.
select stu_name,count(stu_name) from stu_table group by stu_id;
Result
+----------+-----------------+
| stu_name | count(stu_name) |
+----------+-----------------+
| Komal | 4 |
| Ajay | 6 |
| Rakesh | 5 |
| Bhanu | 5 |
30. | Santosh | 4 |
| Tanuj | 1 |
+----------+-----------------+
Create the Table Stu_Table
SQL statement to create table:
create table Stu_Table(Stu_Id integer(2), Stu_Name varchar(15), Stu_Class varchar(10));
Insert data into Stu_Table
Insert into keywords in SQL add the records or rows to the table 'Stu_Table'.
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10);
insert into Stu_Table values(2,'Ajay',10);
insert into Stu_Table values(3,'Rakesh',10);
insert into Stu_Table values(4,'Bhanu',10);
insert into Stu_Table values(5,'Santosh',10);
insert into Stu_Table values(6,'Tanuj',10);
Stu_Table
Records in the table:
+--------+----------
+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1 | Komal | 10 |
| 2 | Ajay | 10 |
| 3 | Rakesh | 10 |
| 4 | Bhanu | 10 |
| 5 | Santosh | 10 |
| 6 | Tanuj | 10 |
+--------+----------+-----------+
Query
Next, the given below Query want to return the conditionally select the data from a table 'Stu_Table'. For
example, we want to retrieve the maximum value of the 'stu_id' with name search specified in the
'Stu_Name'. In order to overcome, we use the WHERE clause. The Syntax is given below:
select max(stu_id) from stu_table
where stu_name in('komal','ajay','santosh','rakesh');
Result
+-------------+
| max(stu_id) |
+-------------+
| 5 |
+-------------+
Create Table Stu_Table
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10),sub_id varchar(2),marks varchar(3));
Insert Data into Stu_Table
The insert into statement add the records or rows to the table 'Stu_Table'.
insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(3,'Rakesh',10,4,37);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(2,'Ajay',10,5,46);
insert into Stu_Table values(3,'Rakesh',10,5,63);
Stu_Table
+--------+----------+-----------+--------+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
31. | 1 | Komal | 10 | 1 | 45 |
| 2 | Ajay | 10 | 1 | 56 |
| 3 | Rakesh | 10 | 1 | 67 |
| 1 | Komal | 10 | 2 | 47 |
| 2 | Ajay | 10 | 2 | 53 |
| 3 | Rakesh | 10 | 2 | 57 |
| 1 | Komal | 10 | 3 | 45 |
| 2 | Ajay | 10 | 3 | 56 |
| 3 | Rakesh | 10 | 3 | 67 |
| 1 | Komal | 10 | 4 | 65 |
| 2 | Ajay | 10 | 4 | 56 |
| 3 | Rakesh | 10 | 4 | 37 |
| 1 | Komal | 10 | 5 | 65 |
| 2 | Ajay | 10 | 5 | 46 |
| 3 | Rakesh | 10 | 5 | 63 |
+--------+----------+-----------+--------+-------+
Query
The Query return you a records and concatenated values from the field 'marks' from table 'Stu_table'. The
group by return the group records from a table 'Stu_Table'.
select stu_id,stu_name,GROUP_CONCAT(marks)
as marks from stu_table group by stu_id;
Result
+--------+----------+----------------+
| stu_id | stu_name | marks |
+--------+----------+----------------+
| 1 | Komal | 45,65,47,65,45 |
| 2 | Ajay | 46,56,56,53,56 |
| 3 | Rakesh | 67,57,37,67,63 |
+--------+----------+----------------+
Create Table Stu_Table
SQL statement to create table:
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15),
Stu_Class varchar(10), sub_id varchar(2), marks varchar(3));
Insert Data into Stu_Table
The insert into add the records or rows to the table 'Stu_Table'.
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10,1,45);
insert into Stu_Table values(2,'Ajay',10,1,56);
insert into Stu_Table values(3,'Rakesh',10,1,67);
insert into Stu_Table values(1,'Komal',10,2,47);
insert into Stu_Table values(2,'Ajay',10,2,53);
insert into Stu_Table values(3,'Rakesh',10,2,57);
insert into Stu_Table values(1,'Komal',10,3,45);
insert into Stu_Table values(2,'Ajay',10,3,56);
insert into Stu_Table values(3,'Rakesh',10,3,67);
insert into Stu_Table values(1,'Komal',10,4,65);
insert into Stu_Table values(2,'Ajay',10,4,56);
insert into Stu_Table values(3,'Rakesh',10,4,37);
insert into Stu_Table values(1,'Komal',10,5,65);
insert into Stu_Table values(2,'Ajay',10,5,46);
insert into Stu_Table values(3,'Rakesh',10,5,63);
Stu_Table
Records in the table:
+--------+----------+-----------+--------+-------+
| Stu_Id | Stu_Name | Stu_Class | sub_id | marks |
+--------+----------+-----------+--------+-------+
| 1 | Komal | 10 | 1 | 45 |
| 2 | Ajay | 10 | 1 | 56 |
| 3 | Rakesh | 10 | 1 | 67 |
| 1 | Komal | 10 | 2 | 47 |
| 2 | Ajay | 10 | 2 | 53 |
| 3 | Rakesh | 10 | 2 | 57 |
| 1 | Komal | 10 | 3 | 45 |
| 2 | Ajay | 10 | 3 | 56 |
| 3 | Rakesh | 10 | 3 | 67 |
32. | 1 | Komal | 10 | 4 | 65 |
| 2 | Ajay | 10 | 4 | 56 |
| 3 | Rakesh | 10 | 4 | 37 |
| 1 | Komal | 10 | 5 | 65 |
| 2 | Ajay | 10 | 5 | 46 |
| 3 | Rakesh | 10 | 5 | 63 |
+--------+----------+-----------+--------+-------+
Drop Table Query
The Drop Table Query delete the table 'Stu_Table' from database. The Table 'Stu_Table' is no longer to be
present in database.
Drop Table Stu_Table;