0% found this document useful (0 votes)
1 views

Complete SQL

The document provides a comprehensive overview of SQL statements and commands used for database management, including creating tables, inserting data, querying data, and manipulating records. It covers various SQL operations such as SELECT, INSERT, UPDATE, DELETE, and JOINs, along with examples and syntax for each operation. Additionally, it explains SQL constraints, aggregate functions, and advanced querying techniques like GROUP BY and CASE statements.

Uploaded by

sohel aaga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Complete SQL

The document provides a comprehensive overview of SQL statements and commands used for database management, including creating tables, inserting data, querying data, and manipulating records. It covers various SQL operations such as SELECT, INSERT, UPDATE, DELETE, and JOINs, along with examples and syntax for each operation. Additionally, it explains SQL constraints, aggregate functions, and advanced querying techniques like GROUP BY and CASE statements.

Uploaded by

sohel aaga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

TABLE

Employees:

Studentss:

Create Table : it is a SQL Statement use to create table in Database

Syntax : Create Table table_name (Column_Name datatype(),Column_Name datatype(),…………..)

Example : Create table Vemployee(EmpoName char(20),EmpoId varchar(20),EmpoNO int,EmpoSalary


int)

Insert Into : it is a SQl statement used to insert values / Records In table

Syantax : Insert into Table_name values (value1 , value 2, value3……..)

Example : insert into Vemployee Values('Ashish','E1234',257535654,40000)

Select : it is a SQL statement use to fetch data / columns data or whole table data from database

1. To fetch whole data


Syntax : Select * From Table_name
EX: select * from Vemployee
2. To Fetch Specific records
Syntax : select Column_name from table_name
EX: select emponame from Vemployee

Distinct : it is a SQL statement use to show unique values from particular column
Syntax : Select Distinct column_name from table_name
EX: select distinct emposalary from Vemployee

Order By : it is a SQL statement use to make order of data in Ascending & Descending order
Syntax : Select * From Table_name order by Column_name ( For Ascending)
Select * From Table_name order by Column_name Desc (For Descending)

Ex : select emponame from Vemployee order by emponame ( For Ascending)


select emponame from Vemployee order by emponame Desc For Descending)

Where Clause: it is a SQL statement use to extract those records which fulfill condition
Syntax: SELECT * from Table_name where column_name =<>!= value ;

EX: SELECT * from Studentss where city = US;

AND: it is a SQL Statement use to select records when both conditions are satisfy. Returns TRUE if both
component conditions are TRUE. Returns FALSE if either is FALSE; otherwise returns UNKNOWN.

Syntax: SELECT column1, column2 FROM table_Name where Condition 1 AND Condition 2;

EX : SELECT Salary, City FROM Employees where FirstName ='yogesh' AND LastName ='patil';

O/P :

OR : it is a SQL Statement use to select records when both condition are true or one condition is true.

Syntax: SELECT column1, column2 FROM table_Name where Condition 1 OR Condition 2;

EX : SELECT Salary, City FROM Employees where FirstName ='yogesh' AND LastName ='Shree';
O/P :

Like: it is a SQL Statement use to search specific condition data in column.


Syntax: 1. For Starting with specific alphabet

Select * From Table_name where column_name like ‘alphabet%’;

Ex: Select * From Employees where City like 'P%';

O/P:

2. For Ending with specific alphabet

Select * From Table_name where column_name like ‘%alphabet’;

Ex: Select * From Employees where City like '%i';

O/P:

3. For Starting with specific alphabets

Select * From Table_name where column_name like ‘alphabets%’;

Ex: Select * From Employees where City like 'Pun%';

O/P:

4. For Ending with specific alphabets

Select * From Table_name where column_name like ‘%alphabet’;


Ex: Select * From Employees where City like '%ai';

O/P:

Update : it is SQL statement use to update/Change or use to update old records with new records
Syntax: UPDATE Table_Name set column Name = value where column Name = value ;

EX: UPDATE Employees SET SALARY=55000 where FIRSTNAME='Ankush';

O/P:

DELETE: it is a SQL command use to delete specific record or all records from table
Syntax: 1. to delete all records

Delete FROM table name;

EX: DELETE FROM Employees;

2. To delete specific record

Delete from table name where column_name =value;

EX: DELETE FROM Employees where Salary = 40000;

DROP: To delete Whole Table

Syntax: Drop Table Table _Name;

Truncate: All data delete but Structure remains same

Syntax: TRUNCATE TABLE Table_Name;

Wild Card: it is SQL Statement use to find data in column


Syntax: select * from Table_Name where column name LIKE ‘alphabet’;
EX: Select * From Employees where City LIKE 'S_____';

O/P:

IN: it is a SQL Statement use to select those records which specify in query.

Syntax: Select*From Table_Name where column_name IN (value1, value2, value3);

EX: SELECT *FROM Employees where E_ID IN (1, 2, 4);

O/P:

BETWEEN: it is a SQL Statement use to select value between ranges which specified.

Syntax: Select*From Table_Name Where Column_Name BETWEEN value1 AND Value2

EX: SELECT *FROM Employees where E_ID BETWEEN 1 AND 2;

O/P:

ALIAS: it is a SQL Statement use to Change column or table name temporally,with or without
changing in database.

Syntax: Select Old_Column_name as New_Column_name from Table_Name;

EX: select MO_No as MobileNo FROM EMPLOYEES ;

O/P:
Union: it is a SQL Statement use to Show same named column name as one from Different table .
Syntax: Select Common_Column_Name1 FROM Table_Name1 UNION Select Common_Column_Name2
FROM Table_Name2;

EX: Select FirstName from Employees UNION Select FirstName from Studentss;

O/P:

SQL Constrains: SQL constrains are used to specify rules for data in a table.
Constrains are: 1) NOT Null
2) NULL
3) UNIQUE KEY
4) Primary Key
5) Foreign Key
6) Default
7) Check
1. NOT Null/NULL: The NOT NULL/NULL is SQL constraint enforces a column to
NOT accept NULL values or to Accept Null Values.
Syntax: Create Table Table_Name(Column_Name Datatype NOT NULL,
Column_Name Datatype NOT NULL);

EX: CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar (255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int);

2. UNIQUE: UNIQUE is A SQL Constrains Which Ensure That All the


Values in a Column is Unique/Different.
Syntax: Create Table Table_Name(Column_Name Datatype NOT NULL UNIQUE,
Column_Name Datatype NOT NULL);

EX: CREATE TABLE Persons (


ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int);

3. PRIMARY: Primary Key is SQL constraint which shows unique values


& primary key cannot be NULL.

Syntax: Create Table Table_Name (Column_Name Datatype NOT NULL UNIQUE,


Column_Name Datatype NOT NULL, PRIMARY KEY (Column_Name));
Ex: CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID));
4. Foreign Key: A foreign key is SQL constraint use to link two tables together.
Syntax: Create Table Table_Name (Column_Name Datatype NOT NULL UNIQUE,
Column_Name Datatype NOT NULL, FOREIGN KEY (Column_Name));
EX: CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
FOREIGN KEY (ID) REFERENCES Persons(ID));
5. Check: it is SQL constraint use to limit value entries in a particular column.
Syntax: Create Table Table_Name (Column_Name Datatype NOT NULL UNIQUE,
Column_Name Datatype NOT NULL, CHECK (Column_Name><=!= value));
EX: CREATE TABLE Personss1 (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);

SQL JOINS: Joins Are SQL Statements Use to join two or more table’s base on
Primary and Foreign key.

Table Persons:

LEFT TABLE

Table Orders:

RIGHT TABLE


• Types Of Join

• Inner Join
• Left Join
• Right Join
• Full Join
• Inner Join: Inner Join is a SQL Statement which is use to show records in
columns which are related to common values.

Syntax: Select table_Name.column_Name, table_Name.Column_Name From


Table 1 Name INNER JOIN Table 2 Name ON Table
1.Common_Column_Name(primary key) =Table 2.
Common_Column_Name(foreign key);

EX: SELECT Persons.firstname, orders.ordernumber FROM persons Inner JOIN


orders ON persons.id = orders.id;

O/P:

• Left Join: Left join is SQL statement which writes all column
from the left table and common matching from the right table
(All column from the left table include those records which do
not have common records in right table)

Syntax: Select table_Name.column_Name, table_Name.Column_Name From


Table 1 Name LEFT JOIN Table 2 Name ON Table 1.Common_Column_Name
=Table 2. Common_Column_Name;

EX: SELECT Persons.firstname, orders.ordernumber FROM persons LEFT JOIN


orders ON persons.id = orders.id;
O/P:

• Right Join: Right join is SQL Statement which writes the all values from right
table and only common values from left table.

Syntax: Select table_Name.column_Name, table_Name.Column_Name From


Table 1 Name RIGHT JOIN Table 2 Name ON Table 1.Common_Column_Name
=Table 2. Common_Column_Name;
EX: SELECT Persons.firstname, orders.ordernumber FROM persons RIGHT JOIN
orders ON persons.id = orders.id;
O/P:

4.Full Join: Full Join is a SQL statement which writes all the column from Left table
and Right table.
Syntax: Syntax: Select table_Name.column_Name,
table_Name.Column_Name From Table 1 Name FULL JOIN Table 2 Name ON
Table 1.Common_Column_Name =Table 2. Common_Column_Name;

EX: SELECT Persons.firstname, orders.ordernumber FROM persons FULL JOIN


orders ON persons.id = orders.id;

O/P:

Aggregate Function: Alter is an aggregate function use to Add, Drop and


Modify column in table.
• Add New Column
Syntax: Alter table table_name
Add column_name Datatype;
EX: alter table Employees Add Address Varchar (20);

O/P:

• Drop Column
Syntax: Alter table table_name
Drop column Column_name;
EX: alter table Employees drop column Address;

O/P:

• Modify Column : it is use to change the datatype of the column.


Syntax: Alter table table_name
Modify Column_name data_type;
EX: alter table Employees modify MO_NO datatype;

• MIN: it is a SQL statement which is use to select minimum value from column.
Syntax: Select min (Column_name) from table_name;
Ex: select min (salary) from employees;
O/P: 40000

• MAX: it is a SQL statement which is use to select maximum value from column.
Syntax: Select max (Column_name) from table_name;
Ex: select max (salary) from employees;
O/P: 90000

• COUNT: it is a SQL statement which is use to select Count of values from


column.
Syntax: Select count (Column_name) from table_name;
Ex: select count (salary) from employees;
O/P: 5

• Avg: it is a SQL statement use to show the avrage of values present in particular
column
Syntax: Select Avg (Column_name) from table_name;
EX: select AVG (Salary)from employees;
O/P:74000

• SUM:it is a SQL statement use to make sum of values in particular column.


Syntax:Select SUM (Column_name) from table_name;
EX: select sum(salary) from employees;

• FIRST:it is a SQL aggregate function use to select first value in column.


Syntax: select First(Column_name) from table_name;
EX:select First(salary) from employees;

• Last:it a SQL aggregate function use to select last value from particular column.
Syntax:select Last(Column_name) from table_name;
EX:select last(salary) from employees;

Group By: it is a SQL statement use to group the values & Use with combination
of aggregate function
Syntax: select agg.function (column_name) from table_name GROUP BY
column_name;
EX: Table Empo11
select sum(salary)from empo11 group by E_ID;
O/P:

Having: it is a SQL statement use to select records which satisfy the given
condition & it is use with aggregate function.
Syntax:select agg.function (column_name) from table_name GROUP BY
column_name HAVING agg.function (column_name)><= value;
EX: select sum(salary)from empo11 group by E_ID having sum(salary)>= 90000;
O/P:

CASE Statement: case statement are SQL statement use to write values
insted of old values present in table.
Syntax: Select CASE
when column_name= 'value' then 'value'
when column_name='value' then 'value'
else 'value' END AS column_name from table_name;
EX: Table empo1
select case when salary='60000' then '70000' when salary='80000' then '90000'
else '10000' end as salary from empo1;
O/P:

*** Query to find the name of person having highest salary


select firstName from Table_name where salary IN(select max(salary) from
table_name);

*** Query to find 1st highest salary


select max(salary) from table_name;

*** Query to find 2nd highest salary


select max(salary)from table_name where salary NOT IN (select max(salary)from
table_name));

*** Query to find Nth highest salary


select salary from table_name T1 where N-1= (select count(distinct salary)from
table_name T2 where T2.salary > T1.salary);
Where T1 & T2 = is alise name
Ex: for 3rd highest salary place N value as 3 i.e 3-1=2

You might also like