Complete SQL
Complete SQL
Employees:
Studentss:
Select : it is a SQL statement use to fetch data / columns data or whole table data from database
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)
Where Clause: it is a SQL statement use to extract those records which fulfill condition
Syntax: SELECT * from Table_name where column_name =<>!= value ;
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.
EX : SELECT Salary, City FROM Employees where FirstName ='yogesh' AND LastName ='Shree';
O/P :
O/P:
O/P:
O/P:
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 ;
O/P:
DELETE: it is a SQL command use to delete specific record or all records from table
Syntax: 1. to delete all records
O/P:
IN: it is a SQL Statement use to select those records which specify in query.
O/P:
BETWEEN: it is a SQL Statement use to select value between ranges which specified.
O/P:
ALIAS: it is a SQL Statement use to Change column or table name temporally,with or without
changing in database.
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);
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.
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)
• Right Join: Right join is SQL Statement which writes the all values from right
table and only common values from left table.
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;
O/P:
O/P:
• Drop Column
Syntax: Alter table table_name
Drop column Column_name;
EX: alter table Employees drop column Address;
O/P:
• 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
• 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
• 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: