ahs sql server
TRANSCRIPT
-
8/4/2019 AHS SQL Server
1/29
MS - SQL SERVER
TOPICSCategorization of Commands
Commands with Examples
Joins
Types of Joins with examplesFAQ s
www.huconsolutions.com
-
8/4/2019 AHS SQL Server
2/29
D ML D ata Manipulation Language
DD L D ata D efinition LanguageD CL D ata Control LanguageTCL Transaction Control Language
Categorization of Commands
-
8/4/2019 AHS SQL Server
3/29
SelectInsertUpdateD elete
DML D ata M anipulation Language
DDL D ata D efinition Language
CreateAlterD rop
Truncate
-
8/4/2019 AHS SQL Server
4/29
G rantRevokeD eny
D CL D ata Control Language
TCL Transaction Control Language
CommitRollback
-
8/4/2019 AHS SQL Server
5/29
Select
Syntax(diff Formats) : Select column_name(s)FROM table_nameSE LECT *FROM table_name
Ex(diff Formats):Select eid, ename, sal from emp
Select * from empN ote :Table names, Column names, keywords are case insensitive.
Eid Ename Sal
1 Paul 12000
2 Anshuman 10000
3 Ambrish 120004 Neethika 8000
Emp
DML Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
6/29
Insert
Syntax (2 formats):Insert into table_namevalues(val1,val2,val3 ,valn)Insert into table_name(col1,col2,col3 coln)values(val1,val2,val3 ,valn)
Ex(2 formats):Insert into emp values(5, Swamy ,7000)Insert into emp(eid,ename,sal) values(5, Swamy ,7000)
Eid Ename Sal
1 Paul 12000
2 Anshuman 10000
3 Ambrish 12000
4 Neethika 8000
5 Swamy 7000
Emp
DML Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
7/29
UpdateSyntax :
update table_nameset col1=val1, col2=val2, coln=valn
where col_name=value
Ex(2 Formats):Updating single column(only sal)The below query allows you to set the sal of Swamy i.e; eid of 5 to new value 7500.
U pdate empset sal=7500 where eid=5
Updating multiple columns(both ename & sal)The below query allows you to set the sal to 7500 and ename to Swaminath of the employee
with eid =5.U pdate empset sal=7500,ename= Swaminath where eid=5
Eid Ename Sal1 Paul 12000
2 Anshuman 10000
3 Ambrish 12000
4 Neethika 8000
5 Swaminath 7500
Emp
DML Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
8/29
D eleteSyntax :
D elete table_name where col_name=valueD elete from table_name where col_name=value
Ex(2 Formats):D eleting a selected row(by its eid)The below query allows you to delete the row with eid=5.
D elete from emp where eid=5 OR
D elete emp where eid=5
D eleting all records/rows/tuplesThe below query allows you to delete all rows from emp table.
D elete emp
Eid Ename Sal1 Paul 12000
2 Anshuman 10000
3 Ambrish 12000
4 Neethika 8000
5 Swaminath 7500
Emp
DML Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
9/29
W here, Between , A ND , OR , IN , UN ION , UN ION ALL , IN TERSECT , ANY ,LIKE, TOP,G roup by, Order by, Having
< , > , = , , =
Not Equal, insome versions it is !=
Operators, Conditional Clauses, Keywords,Predefined Functions .
-
8/4/2019 AHS SQL Server
10/29
SELECT FROM W HERE AND SELECT FROM W HERE OR SELECT
FROM W HEREBETW EEN AND
SELECT * FROM empW HERE sal>9000
AND
sal
-
8/4/2019 AHS SQL Server
11/29
SELECT FROM W HERE
IN
( , )
SELECT FROM W HERELIKE patternSELECT column_name(s)FROM table_nameORD ER BY column_name [A SC| D ESC]
Retrieving the records from emp tablewhose eid in 1,2SELECT * FROM empW HERE eidIN (2 ,4)
Retrieving the records from emp tablewhose ename starts with ASELECT * FROM empW HERE ename
LIKE A%
Retrieving the records of employees fromemp table with sal in descending orderSELECT * FROM empORD ER BY sal desc
Syntax Exa mples
Syntax & Ex : Operators, Conditional Clauses .
-
8/4/2019 AHS SQL Server
12/29
Retrieving min value for a column from tableSELECT min(column_name)
FROM
Retrieving max value for a column from tableSELECT max(column_name)FROM
Retrieving count of rows/records from tableSELECT COU NT(*)FROM
Retrieving min sal from empSELECT min(sal)
FROM emp
Retrieving max sal from empSELECT max(sal)FROM emp
Retrieving count of rows/records from tableSELECT COU NT(*)FROM emp
Syntax Exa mples
Syntax & Ex : Predefined Functions, Keywords,Clauses ..
-
8/4/2019 AHS SQL Server
13/29
SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueG ROUP BY column_name
SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator value
GROU P BY column_nameHAVING aggregate_function(column_name) operator value
Syntax
Syntax & Ex : Predefined Functions, Keywords,Clauses ..
-
8/4/2019 AHS SQL Server
14/29
CreateSyntax :
CREATE TABLE table_name(col1 data_type [Primary Key] [ Identity(startval,incr)],col2 data_type,col3 data_type,...)
Ex(2 Formats):The below format helps us to create structure of table emp with 3 columns eid,ename,salCreate table emp(eid int , ename varchar(50), sal int)The below format helps us to create structure of table emp with 3 columns eid,ename,sal with
eid set to primary key and identity(1,1) i.e; with startval 1 and auto-increment of 1Create table emp(eid int Primary Key Identity(1,1), ename varchar(50), sal int)
Eid Ename Sal
Emp
DDL Commands with Syntax & Ex
Eid is of type int
with PK andIdentity set
-
8/4/2019 AHS SQL Server
15/29
D ropSyntax :
D ROP table table_name
Ex(2 Formats):The below query helps us to drop the table emp(the records inside thetable-if any and the structure of table) but does not reset the identityspecified on a column to its initial value.
D rop table emp
DDL Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
16/29
TruncateSyntax :
Truncate table table_name
Ex(2 Formats):The below query helps us to drop the table emp(the records inside thetable-if any and the structure of table) and resets the identity specified ona column to its initial value.
Truncate table emp
DDL Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
17/29
AlterSyntax to Add a new Column
ALTER TABLE table_nameADD column_name datatype
Syntax to D rop existing Column
ALTER TABLE table_nameD ROP COLU M N column_name
Ex(Add Column):The below format helps us to add new column desg to existing table emp.
ALTER TABLE Emp
ADD
desg varchar(50)Ex(D rop Column):The below format helps us to drop existing column desg from table emp.
ALTER TABLE EmpD ROP COLU M N desg
Eid Ename Sal D esg
Emp
DDL Commands with Syntax & Ex
-
8/4/2019 AHS SQL Server
18/29
The JO IN keyword in SQ L Server helps to query datafrom two or more tables, based on a relationship
between certain columns in the tables joined with acondition.Join : Join helps us to join two or more tables andfetches matching records from the tables joined that
have a common column.Typically a JOIN is used in conjunction with ONkeyword for filtering data.
JOIN S
-
8/4/2019 AHS SQL Server
19/29
SQL JOIN S
INN ER JOIN
OUTER JOIN (3 Categories)Left Outer Join / Left JoinRight Outer Join / Right JoinFull Outer Join / Full Join
CROSS JOIN
SELF JOIN
-
8/4/2019 AHS SQL Server
20/29
JOIN (INN ER JOIN ): Returns set of rows when there is atleast one match in both tablesLEFT JOIN (Left Outer Join) : Return all rows from the left
table, even if there are no matches in the right table byassigning a N U LL value to the columns in the right table.RIG HT JOIN (Right Outer Join) : Return all rows from theright table, even if there are no matches in the left table
by assigning a N U LL value to the columns in the left table.FULL JOIN (Full Outer Join) : Return rows when there is amatch in one of the tables i.e; Left Join + Right Join.
D ifferent Types of JO INS
-
8/4/2019 AHS SQL Server
21/29
Table Structure
PId Last N ame First N ame Address City
1 Paul Joseph Hitech City Hyderabd
2 Samineni Anthony Banjara Hills Hyderabad
3 Nitty Carmel SP Road Secunderabad
OId Order N o PId
1 778 9 5 32 44678 3
3 22456 1
4 24562 1
5 34764 15
Products
O rd e r s
-
8/4/2019 AHS SQL Server
22/29
The INNER JOIN keyword return rows when there isat least one match in both tables.INNER JOIN is same as JO IN which retrievesmatching records from two or more tables.
SELECT column_name(s)FROM table_name1INN ER JOIN table_name2
ON
table_name1.column_name=table_name2.column_name
INN ER JOIN
-
8/4/2019 AHS SQL Server
23/29
SQL INN ER JOIN Example
SELECT Persons. LastName,Persons.FirstName, Orders.OrderNoFROM PersonsINN ER JOIN OrdersON Persons.P Id=Orders.P IdORD ER BY Persons. LastName
Last N ame First N ame Order N o
Paul Joseph 22456Paul Joseph 24562
Nitty Carmel 778 9 5
Nitty Carmel 44678
-
8/4/2019 AHS SQL Server
24/29
The LEFT JOIN keyword returns all rows from the left table(table_name1), even if there are no matches in the right table(table_name2).In short, LEFT JOIN keyword helps us to return the matching
rows from both the tables and non-matching rows from leftside table.In some databases LEFT JOIN is called LEFT OU TER JOIN
SELECT column_name(s)FROM table_name1LEFT JOIN table_name2ONtable_name1.column_name=table_name2.column_name
LEFT JOIN
-
8/4/2019 AHS SQL Server
25/29
SELECT Persons. LastName, Persons.FirstName,Orders.OrderNoFROM PersonsLEFT JOIN Orders
ON
Persons.PId=Orders.P
IdORD ER BY Persons. LastName
LEFT JOIN O/P
Last N ame First N ame Order N o
Paul Joseph 22456
Paul Joseph 24562Nitty Carmel 778 9 5
Nitty Carmel 44678
Samineni Anthony N U LL
-
8/4/2019 AHS SQL Server
26/29
The R IGHT JOIN keyword returns all rows from the Right table(table_name2), even if there are no matches in the left table(table_name1).In short, R IGHT JOIN keyword helps us to return the matching
rows from both the tables and non-matching rows from rightside table.In some databases R IGHT JOIN is called R IGHT OU TER JOIN
SELECT column_name(s)FROM table_name1RIG HT JOIN table_name2ONtable_name1.column_name=table_name2.column_name
RIG HT JOIN
-
8/4/2019 AHS SQL Server
27/29
SELECT Persons. LastName, Persons.FirstName,Orders.OrderNoFROM PersonsRIG HT JOIN Orders
ON
Persons.PId=Orders.P
IdORD ER BY Persons. LastName
RIG HT JOIN O/P
Last N ame First N ame Order N o
Paul Joseph 22456
Paul Joseph 24562Nitty Carmel 778 9 5
Nitty Carmel 44678NU LL NU LL 34764
-
8/4/2019 AHS SQL Server
28/29
The FU LL JOIN keyword return rows when there is a match in
one of the tables .
SE LECT column_name(s)FROM table_name1FULL JOIN table_name2ONtable_name1.column_name=table_name2.column_n
ame
FULL JOIN
-
8/4/2019 AHS SQL Server
29/29
Last N ame First N ame Order N o
Paul Joseph 22456
Paul Joseph 24562Nitty Carmel 778 9 5
Nitty Carmel 44678
Samineni Anthony N U LL
NU LL NU LL 34764
SELECT Persons. LastName, Persons.FirstName,Orders.OrderNoFROM PersonsFULL JOIN Orders
ON
Persons.PId=Orders.P
IdORD ER BY Persons. LastName
FULL JOIN O/P