SQL DELETE Statement

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 25000
Ramesh 35 Delhi 25000
Kaushik 31 Indore 25000
Hardik 22 Hyderabad 25000
Glory 28 Kanpur 25000
Akshaya 29 Delhi 25000
DSouza 38 Bangalore 25000

DELETE:

DELETE from employeeinfo where name=’DSouza’;

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 25000
Ramesh 35 Delhi 25000
Kaushik 31 Indore 25000
Hardik 22 Hyderabad 25000
Glory 28 Kanpur 25000
Akshaya 29 Delhi 25000

DELETE from employeeinfo;

employeeinfo table got deleted.

SQL UPDATE Statement

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000
DSouza 38 Bangalore 15000

UPDATE Statement:

UPDATE employeeinfo SET salary=’25000′;

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 25000
Ramesh 35 Delhi 25000
Kaushik 31 Indore 25000
Hardik 22 Hyderabad 25000
Glory 28 Kanpur 25000
Akshaya 29 Delhi 25000
DSouza 38 Bangalore 25000

SQL INSERT INTO Statement

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

INSERT INTO:

INSERT INTO employeeinfo values(‘DSouza’,’38’,’Bangalore’,’15000′);

SELECT * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Glory 28 Kanpur 6500
Akshaya 29 Delhi 5000
DSouza 38 Bangalore 15000

SQL ORDER BY Keyword

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

ORDER By:

select * from employeeinfo ORDER BY name ASC;

name age address salary
Akshaya 29 Delhi 5000
Glory 28 Kanpur 6500
Hardik 22 Hyderabad 8500
Kaushik 31 Indore 10000
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500

select * from employeeinfo ORDER BY name DESC;

name age address salary
Ramesh 35 Delhi 4500
Komal 25 Mumbai 5000
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Glory 28 Kanpur 6500
Akshaya 29 Delhi 5000

SQL AND & OR Operators

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

AND Operator:

SELECT * from employeeinfo where name=’Ramesh’ AND age=’35’;

name age address salary
Ramesh 35 Delhi 4500

OR Operator:

SELECT * from employeeinfo where name=’Ramesh’ OR name =’Komal’;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500

Both AND & OR Operator:

select * from employeeinfo where name=’Ramesh’ AND (age=’35’ OR age=’25’);

name age address salary
Ramesh 35 Delhi 4500

SQL WHERE Clause

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

WHERE Clause:

select * from employeeinfo where name=’Komal’;

name age address salary
Komal 25 Mumbai 5000

select name, age from employeeinfo where name=’Komal’;

name age
Komal 25

SQL DISTINCT Statement

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

DISTINCT Statement:

SELECT DISTINCT salary from employeeinfo;

salary
5000
4500
10000
8500
6500

SQL SELECT Statement

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

SELECT Statement:

SELECT * from employeeinfo where name=’Ramesh’;

name age address salary
Ramesh 35 Delhi 4500

SELECT name,age from employeeinfo;

name age
Komal 25
Ramesh 35
Kaushik 31
Hardik 22
Glory 28
Akshaya 29

SQL Create Table and Insert Data

CREATE Table:

create table employeeinfo(name varchar(20), age varchar(10), address varchar(40), salary varchar(50));

SHOW Tables:

show tables;

Tables_in_studentinfo
employeeinfo

INSERT Data:

insert into employeeinfo values(‘Komal’,’25’,’Mumbai’,’5000′);

SHOW Table:

select * from employeeinfo;

name age address salary
Komal 25 Mumbai 5000
Ramesh 35 Delhi 4500
Kaushik 31 Indore 10000
Hardik 22 Hyderabad 8500
Aishwarya 28 Bangalore 6500
Akshaya 29 Delhi 5000

 

SQL Constraints

Constraints are the rules enforced on data columns on table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database.

Commonly used constraints are:

  • NOT NULL Constraint: Ensures that a column cannot have NULL value.
  • DEFAULT Constraint: Provides a default value for a column when none is specified.
  • UNIQUE Constraint: Ensures that all values in a column are different.
  • PRIMARY Key: Uniquely identified each rows/records in a database table.
  • FOREIGN Key: Uniquely identified a rows/records in any another database table.
  • CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain conditions.
  • INDEX: Use to create and retrieve data from the database very quickly.
1 2 3