You are on page 1of 19

SQL – Structured Query Language

1- SQL is a standard language for storing, manipulating and


retrieving data from the database.
2- SQL keywords are not case sensitive. Eg. Select as SELECT
3- Semicolon is the standard way to separate each SQL
statement in database system.
Eg – Select * from TN;

What SQL can do?

1- SQL can Insert records in database.


2- SQL can Update records in database.
3- SQL can Delete records in database.
4- SQL can create the tables in database.
5- SQL can alter the table in database.
6- SQL can retrieve the data from the database.

Database – A database in SQL server is made up of a collection


of tables that stores a specific set of data.

Table – Table contains rows and columns, where the rows are
known as records and the column are known as fileds.

SR.no Firstname Mobile City Mockmarks


number
1 Himesh 9988776655 Mumbai 15
2 Ramesh 9878876548 Pune 18
3 Kalpesh 9876543456 Delhi 16

SQL is categorized into different types of commands


1- DDL – Data Definition Language
2- DML – Data Manipulation Language
3- DQL – Data Query Language
4- DCL – Data Control Language
5- TCL – Transaction Control Language
DDL – Data Definition Language
1- All DDL commands are auto-committed that means it
permanently save all the changes in database.
2- The Basic DDL command in SQL are Create table, Alter
table, Drop table and Truncate table

Command What it does?


Create table It creates new table
Drop Table It deletes entire table from database
Alter It Modifies the Existing table
Truncate Deletes the data inside a table but not the
table itself.(Column /Structure will remain
same)

DML – Data Manipulation Language


1- DML commands are used to modify the database
2- The DML commands in SQL are Insert, Update and Delete

Commands What it Does?


Insert Add new information to the database/table
Update Modifies the information Currently stored in
the database/table
Delete Delete particular Information From the
database / table

DQL – Data Query Language


1- DQL is used to fetch the data from the database.
2- DQL has only one command that is ‘SELECT’

Command What it does?


Select It retrieves the data /information from the
database/table.

DCL – Data Control Language


DCL commands are used to grant and take back the authority
from any database user.

1- GRANT – It is used to give access to a database.


2- Revoke – It is used to take back the permission from user.

TCL – Transaction control language

1- Commit – Commit command is used to save all the


transaction to the database.
2- Rollback – Rollback command is used to undo the
transactions that have not already been saved to the
database.

Data types in SQL

Character/String

1- Char(20) – Fixed length


2- Varchar(255) – Variable length

Numbers

1- Int
2- Bigint

Values – The value command specifies the value of an insert into


statement.

Create Table Student(

Studentid int,

Firstname Varchar(255),

Lastname Varchar(255),

Mockmarks int,

City varchar(255)
);

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (1,'Vikas','Mishra',18,'Palghar');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (2,'Jyoti','Chavhan',16,'Amravati');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (3,'Amey','Kalantre',14,'Kolhapur');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (4,'Yogesh','Nangare',19,'Ahmednagar');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (5,'Prajakta','Tidar',17,'Satara');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (6,'Rohini','Kole',19,'Sangali');

Select * from Student;

Drop – It deletes the entire table

Syntax – Drop table TN;

Drop table Student;


Truncate – Deletes the data inside the table but not the table
itself.

Syntax – Truncate table TN;

Truncate table Student;

Alter – Modifies the existing table

Syntax – alter table TN add CN datatype;

Alter table student add pincode int;

Update – Modifies the information currently stored in the table.

Syntax- update TN set CN=value where condition;

update student set pincode=401501 where studentid=1;

Delete – Deletes particular records from the database.

Syntax – Delete From TN where condition;

delete from student where city='Amravati';


Where Clause

It is used to specify a condition while fetching the data from a


single table.

Syntax – Select * from TN where condition;

select * from Student where mockmarks=18

Logical Operators – AND, OR, NOT

1- AND – It is used to filter the records more than one


condition. While Using AND operator both the conditions
must be true.

Syntax – Select * from TN where CN1=Condition and


CN2=Condition;

select * from Student where mockmarks=19 and city='Sangali';

2- OR – It displays a record on the basis of condition and it is


separated by OR operator. In OR operator one condition
must be true then it will show the result.

Syntax – Select * from TN where CN1=condition Or


CN2=condition;

select * from Student where mockmarks=18 or city='Sangali';

3- NOT – If we don’t want a particular data in a table we are


using NOT operator.

Syntax – Select * from TN where Not CN1=Condition;

select * from Student where mockmarks=18 or city='Sangali';

Arithmetic Operators

1- Addition (+) – It is used to perform addition operation on


the data items.
Syntax- Select CN+value from TN;
select mockmarks+3 from Student

2- Subtraction (-) – It is used to perform Subtraction operation


on the data items.
Syntax- Select CN-value from TN;
select mockmarks-3 from Student

3- Multiplication (*) – It is used to perform Multiplication


operation on the data items.
Syntax- Select CN*value from TN;
select mockmarks*3 from Student

4- Division (/) – It is used to perform division operation on the


data items.
Syntax- Select CN/value from TN;
select mockmarks/3 from Student

5- Modulus (mod,%) – It is used to get reminder when one


data is divided by another
Syntax- Select Mod(CN,Value) from TN;
Select Mod(Mockmarks,3) from Student.

Comparison Operators

A comparison operators is used to compare two values as per the


applied conditions.

1- Equal to (=)
The = symbol is used to filter results that equals to a certain
value.
Syntax- Select * from TN where CN=value;
Select * from Student where mockmarks=18;
2- Not Equal to (!=)
The != symbol is used to filter results that do not equals to a
certain value.
Syntax- Select * from TN where CN!=value;
Select * from Student where mockmarks!=18;

3- Greater than (>)


This > symbol is used to filter results where a column value
is greater than the certain value.
Syntax- Select * from TN where CN>value;
Select * from Student where mockmarks>18

4- less than (<)


This < symbol is used to filter results where a column value
is less than the certain value.
Syntax- Select * from TN where CN<value;
Select * from Student where mockmarks<18

5- Greater than or equal to (>=)


This >= symbol is used to filter results where a column
value is greater than or equal to the certain value.
Syntax- Select * from TN where CN>=value;
Select * from Student where mockmarks>=18

6- Less than or equal to (<=)


This <= symbol is used to filter results where a column
value is less than or equal to the certain value.
Syntax- Select * from TN where CN<=value;
Select * from Student where mockmarks<=18

Like Operator

In like operator where clause is used to compare a value to


similar values using wildcard operators.
There are two wildcard operators used in conjunction with the like
operator.

Percent sign (%) Matches one or more characters


Underscore (_) Matches one Character

Syntax – Select * from TN where CN like pattern;

Select * from Student where city like ‘A%’;

Like Operator Description


Where CN like ‘a%’ Finds the values that starts with ‘a’
Where CN like ‘%a’ Finds the values that ends with ‘a’
Where CN like ‘%dn%’ Finds the value that have ‘dn’ at any
position
Where CN like ‘_a%’ Finds the value that have ‘a’ at second
position
Where CN like ‘a%y’ Finds the vales that starts with ‘a’ and
ends with ‘y’
Where CN like ‘%a_’ Finds the values that have ‘a’ at second
last position

Not Like operator

Sometimes we want to get the records that doesn’t match the


like pattern in that case we can use not like operator.

Syntax- Select * from TN where CN not like pattern;

Select * from Student where City not like ‘a%’;

Between Operator

It is used to select the values within agiven range with a where


clause.

Syntax – Select * from TN where CN between value1 and value


2;
select * from Student where mockmarks between 15 and 18;

Not between Operator

It will fetch the data except the applied condition.

Syntax- Select * from TN where CN not between value1 and


value2;

select * from Student where mockmarks not between 15 and 18;

In operator

The in operator allows you to specify multiple values in a where


clause.

Syntax- Select * from TN where CN IN (value1,Value2);

select * from student where mockmarks in(17,14,18);

Distinct Keyword

It is used to show the Unique values from the table.

Syntax – Select distinct CN from TN;

select Distinct City from Student;

TOP/Limit/Rownum

It is used to limit the number of rows returned by a query.

Syntax – Select * from TN where rownum condition;

select * from Student where rownum <=3;

select Top 3 * from TN;

select * from TN limit 3;

Order By Clause
It is used to sort the records in your result set in ascending and
descending order.

Syntax – Select * from TN order by CN asc/desc;

select * from Student order by mockmarks asc;

select * from Student order by mockmarks desc;

Aggregate Functions

1- Count() – Count is used to count the records from the table.


2- Avg () – It shows the average value from the table.
3- Sum () – It is used to for the addition of the values
4- Max () – It shows the max value
5- Min () – It shows the Min value

Group by

It is used to group the data from a table on certain value. The


group by statement groups rows that have same value or similar
value. The group by statement is often used with aggregate
functions.

Syntax – Select CN1,max(CN2) from TN group by CN1;

select Firstname,max(mockmarks) from Student group by


Firstname

Having clause

The having clause places the condition in the groups defined by


the group by clause. This Clause is implemented after the group
by clause in the select statement.

Syntax - Select CN1,max(CN2) from TN group by CN1 having


max(CN2) condition;
select Firstname,max(mockmarks) from Student group by
Firstname having max(mockmarks)>=18;

Note – The having clause was added to SQL because the where
keyword cannot be used with aggregate functions.

SQL Constraints

1- Constraints are used while creation of table.


2- SQL constraints are used to specify the value for data in a
table.
3- Constraints can be used when the table is created with the
create table statement.

Types of constraints

1- Not null
2- Unique
3- Primary Key
4- Foreign key
5- Check constraint
6- Default constraints

Not Null – It does not allow the user to insert null value. If we
want to make any field as mandatory then we will use not null
constraint.

Syntax- Create Table TN(CN1 datatype not null);

Create Table Student(

Studentid int,

Firstname Varchar(255) not null,

Unique – It does not accept duplicate value it only accepts Unique


value.

Syntax – Create table TN(CN1 datatype Unique);


Create Table Student(

Studentid int unique,

Primary key – It is a combination of not null and Unique


constraint. It does not allow duplicate values and also it does not
allows null values. In each table only one primary key is used.

Syntax - Create table TN(CN1 datatype Primary key);

Create Table Student(

Studentid int primary key,

Foreign Key – It is used for mapping the relationship between two


or more table. Primary key of one table acts as foreign key of
another table. It can accept multiple null and duplicate values.

Syntax – Create Table TN2( CN1 datatype , CN2 datatype, CN3


datatype, foreign key(CN) references TN1(CN1));

Create Table Order5(

Orderid int,

Oname Varchar(255),

Studentid int, foreign key(Studentid) references


Student(Studentid)

);

Create Table Student(

Studentid int primary key,

Firstname Varchar(255),

Lastname Varchar(255),

Mockmarks int,
City varchar(255)

);

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (1,'Vikas','Mishra',18,'Palghar');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (2,'Jyoti','Chavhan',16,'Amravati');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (3,'Amey','Kalantre',14,'Kolhapur');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (4,'Yogesh','Nangare',19,'Ahmednagar');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (5,'Prajakta','Tidar',17,'Satara');

insert into
Student(Studentid,Firstname,Lastname,Mockmarks,City)

Values (6,'Rohini','Kole',19,'Sangali');

Select * from Student;

drop table Student

Create Table Order5(


Orderid int,

Oname Varchar(255),

Studentid int, foreign key(Studentid) references


Student(Studentid)

);

insert into order5(Orderid,Oname,Studentid)

Values (1778,'Iphone',4);

insert into order5(Orderid,Studentid)

Values (1767,6);

insert into order5(Orderid,Oname,Studentid)

Values (1654,'NoKia',1);

insert into order5(Orderid,Oname,Studentid)

Values (1743,'Readme',6);

insert into order5(Orderid,Oname,Studentid)

Values (1900,'Vivo',10);

select * from Order5

drop table order5

Check Constraint

1- It is used to restrict the value of column between the range.


2- We can apply check constraint on a particular column that
we want while creating table.

Syntax – Create table TN( CN datatype, check(CN condition));

Create Table Student(


Studentid int, check(Studentid<=100),

Default constraint

1- It is used to set default value


2- If we are not specifying any value for the column the it will
take the default value.

Syntax – Create Table TN(CN1 datatype default value);

Create Table Student(

Studentid int,

Firstname Varchar(255),

Lastname Varchar(255),

Mockmarks int default 10,

Union and Union All

Union

1- It is used to combine the result set of two or more than two


oracle select statement.
2- It combines both select statement and removes the
duplicate values.
3- It gives only unique value.

Syntax – Select CN from TN1 Union Select CN from TN2

Select * from TN1 Union Select * from TN2 (Note – SQL has strict
rule that both the tables must have same number of Columns)

Union All

It allows duplicate values and it combines all the data from both
the tables.

Syntax – Select CN from TN1 Union all Select CN from TN2


SQL Joins

Joins are used to retrieve data from multiple tables. When we


want to combine two or more tables SQL joins are used.

Types of Joins

1- Inner Join
2- Left join
3- Right join
4- Full Join

Syntax – Select CN1(L),CN2(L), CN3(R),CN4(R) from TN1 inner


join TN2 on TN1.CN(C)=TN2.CN(C)

select Firstname,Salary,Managername,Designation from Student


inner join Department on
Student.Studentid=Department.Studentid;

Inner Join – It is the most common type of join. It gives matching


records from both the tables.
Left Join – It returns all records from left table and matching
records from right table.

Right Join - It returns all records from Right table and matching
records from left table.

Full Join – It returns all the records from both the table.

Aliases – Aliases are temporary name given to column for the


purpose of a particular SQL query.

Syntax – Select CN as C from TN;

Interview Questions

1- How to Find the 1st Highest Salary From the table?

select max(salary) from Student;

2- How to find the 2nd Highest Salary From the table?

Select max(salary) from student where Salary <(select


max(salary) from Student)

Select max(salary) from student where Salary not in (select


max(salary) from Student)

3- How to find the 3rd highest salary from the table?

Select Max(salary) from Student Where Salary < (Select


max(salary) from student where Salary not in (select
max(salary) from Student))

4- How to find the nth Highest Salary?

Select * from (select * from (select * from (select * from


Student order by Salary Desc) where Rownum <=2) order by
Salary) where rownum <=1

5- Joins In SQL?
6- Constraints In SQL?
7- Aggregate Functions?
8- Different Types of SQL commands?
9- Difference between Drop, Truncate and delete
10- Difference Between Union and Union all?
11- Different Operators In SQl?
12- Different clauses in SQL?

The END

You might also like