Vous êtes sur la page 1sur 29

TOPICS

Categorization of Commands Commands with Examples Joins Types of Joins with examples FAQ s

MS - SQL SERVER

www.huconsolutions.com

Categorization of Commands

DML DDL DCL TCL

Data Manipulation Language Data Definition Language Data Control Language Transaction Control Language

DML Data Manipulation Language


Select Insert Update Delete

DDL Data Definition Language


Create Alter Drop Truncate

DCL Data Control Language


Grant Revoke Deny

TCL Transaction Control Language


Commit Rollback

DML Commands with Syntax & Ex


Select
Eid Ename Sal 12000 10000 12000 8000

Syntax(diff Formats) :
Select column_name(s) FROM table_name SELECT * FROM table_name Select eid, ename, sal from emp Select * from emp

1 2 3 4

Paul Anshuman Ambrish Neethika

Ex(diff Formats):

Emp

Note :
Table names, Column names, keywords are case insensitive.

DML Commands with Syntax & Ex Insert


Syntax (2 formats):
Eid Ename 1 2 3 Insert into table_name values(val1,val2,val3 ,valn) 4 Insert into table_name(col1,col2,col3 coln) 5 values(val1,val2,val3 ,valn) Paul Anshuman Ambrish Neethika Swamy Emp Sal 12000 10000 12000 8000 7000

Ex(2 formats):
Insert into emp values(5, Swamy ,7000) Insert into emp(eid,ename,sal) values(5, Swamy ,7000)

DML Commands with Syntax & Ex


Update
Eid Ename 1 Paul Sal 12000
Syntax : 2 Anshuman 10000 update table_name set col1=val1, col2=val2, coln=valn 3 Ambrish 12000 where col_name=value 4 Neethika 8000 Ex(2 Formats): 5 Swaminath 7500 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. Emp Update emp set 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. Update emp set sal=7500,ename= Swaminath where eid=5

DML Commands with Syntax & Ex


Delete
Syntax : Delete table_name where col_name=value Delete from table_name where col_name=value

Eid Ename 1 2 3 4 5 Paul Anshuman Ambrish Neethika Swaminath Emp

Sal 12000 10000 12000 8000 7500

Ex(2 Formats): Deleting a selected row(by its eid) The below query allows you to delete the row with eid=5. Delete from emp where eid=5

OR

Delete emp where eid=5 Deleting all records/rows/tuples The below query allows you to delete all rows from emp table. Delete emp

Operators, Conditional Clauses, Keywords, Predefined Functions .

Where, Between , AND, OR , IN , UNION , UNION ALL , INTERSECT , ANY, LIKE, TOP, Group by, Order by, Having < , > , = , <> , <= , >=

<>

Not Equal, in

some versions it is !=

Syntax & Ex : Operators, Conditional Clauses .


Syntax

Examples

SELECT <column_name(s)> FROM <table_name> WHERE <condition1> AND <condition2> SELECT <column_name(s)> FROM <table_name> WHERE <condition1> OR <condition2> SELECT <column_name(s)> FROM <table_name> WHERE <column_name> BETWEEN <value1> AND <value2>

SELECT * FROM emp WHERE sal>9000 AND sal<14000 SELECT * FROM emp WHERE ename= Paul OR ename= Ambrish SELECT * FROM emp WHERE sal BETWEEN 9000 AND 14000

Syntax & Ex : Operators, Conditional Clauses .


Syntax

Examples
Retrieving the records from emp table whose eid in 1,2 SELECT * FROM emp WHERE eid IN (2 ,4) Retrieving the records from emp table whose ename starts with A SELECT * FROM emp WHERE ename LIKE A% Retrieving the records of employees from emp table with sal in descending order SELECT * FROM emp ORDER BY sal desc

SELECT <column_name(s)> FROM <table_name> WHERE <column_name> IN (<value1> ,<value2>) SELECT <column_name(s)> FROM <table_name> WHERE <column_name> LIKE pattern SELECT column_name(s) FROM table_name ORDER BY column_name [ASC|DESC]

Syntax & Ex : Predefined Functions, Keywords, Clauses ..


Syntax
Retrieving min value for a column from table SELECT min(column_name) FROM <table_name> Retrieving max value for a column from table SELECT max(column_name) FROM <table_name> Retrieving count of rows/records from table SELECT COUNT(*) FROM <table_name>

Examples
Retrieving min sal from emp SELECT min(sal) FROM emp Retrieving max sal from emp SELECT max(sal) FROM emp Retrieving count of rows/records from table SELECT COUNT(*) FROM emp

Syntax & Ex : Predefined Functions, Keywords, Clauses ..


Syntax
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value

DDL Commands with Syntax & Ex


Create
Syntax : CREATE TABLE table_name ( col1 data_type [Primary Key] [Identity(startval,incr)], col2 data_type, col3 data_type, ... )

Eid Ename

Sal Emp

Eid is of type int with PK and Identity set

Ex(2 Formats): The below format helps us to create structure of table emp with 3 columns eid,ename,sal Create 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 1 Create table emp (eid int Primary Key Identity(1,1), ename varchar(50), sal int)

DDL Commands with Syntax & Ex


Drop
Syntax : DROP table table_name Ex(2 Formats): The below query helps us to drop the table emp(the records inside the table-if any and the structure of table) but does not reset the identity specified on a column to its initial value. Drop table emp

DDL Commands with Syntax & Ex


Truncate
Syntax : Truncate table table_name Ex(2 Formats): The below query helps us to drop the table emp(the records inside the table-if any and the structure of table) and resets the identity specified on a column to its initial value. Truncate table emp

DDL Commands with Syntax & Ex


Alter
Syntax to Add a new Column ALTER TABLE table_name ADD column_name datatype Syntax to Drop existing Column ALTER TABLE table_name DROP COLUMN 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(Drop Column): The below format helps us to drop existing column desg from table emp. ALTER TABLE Emp DROP COLUMN desg

Eid

Ename

Sal

Desg Emp

JOINS
The JOIN keyword in SQL Server helps to query data from two or more tables, based on a relationship between certain columns in the tables joined with a condition. Join : Join helps us to join two or more tables and fetches matching records from the tables joined that have a common column. Typically a JOIN is used in conjunction with ON keyword for filtering data.

SQL JOINS

INNER JOIN OUTER JOIN(3 Categories) Left Outer Join / Left Join Right Outer Join / Right Join Full Outer Join / Full Join CROSS JOIN SELF JOIN

Different Types of JOINS


JOIN (INNER JOIN): Returns set of rows when there is at least one match in both tables LEFT JOIN(Left Outer Join): Return all rows from the left table, even if there are no matches in the right table by assigning a NULL value to the columns in the right table. RIGHT JOIN(Right Outer Join): Return all rows from the right table, even if there are no matches in the left table by assigning a NULL value to the columns in the left table. FULL JOIN(Full Outer Join): Return rows when there is a match in one of the tables i.e; Left Join + Right Join.

Table Structure
Products

PId LastName FirstName Address 1 2 3


Orders OId 1 2 3 4 5 OrderNo 77895 44678 22456 24562 34764 PId 3 3 1 1 15

City Hyderabd

Paul

Joseph

Hitech City

Samineni Anthony Nitty Carmel

Banjara Hills Hyderabad SP Road Secunderabad

INNER JOIN
The INNER JOIN keyword return rows when there is at least one match in both tables. INNER JOIN is same as JOIN which retrieves matching records from two or more tables.
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name

SQL INNER JOIN Example


SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons INNER JOIN Orders ON Persons.PId=Orders.PId ORDER BY Persons.LastName
LastName Paul Paul Nitty Nitty FirstName Joseph Joseph Carmel Carmel OrderNo 22456 24562 77895 44678

LEFT JOIN
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 left side table. In some databases LEFT JOIN is called LEFT OUTER JOIN

SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

LEFT JOIN O/P


SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.PId=Orders.PId ORDER BY Persons.LastName
LastName Paul Paul Nitty Nitty Samineni FirstName Joseph Joseph Carmel Carmel Anthony OrderNo 22456 24562 77895 44678 NULL

RIGHT JOIN
The RIGHT 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, RIGHT JOIN keyword helps us to return the matching rows from both the tables and non-matching rows from right side table. In some databases RIGHT JOIN is called RIGHT OUTER JOIN

SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

RIGHT JOIN O/P


SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.PId=Orders.PId ORDER BY Persons.LastName
LastName Paul Paul Nitty Nitty
NULL

FirstName Joseph Joseph Carmel Carmel


NULL

OrderNo 22456 24562 77895 44678 34764

FULL JOIN
The FULL JOIN keyword return rows when there is a match in one of the tables.

SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_n ame

FULL JOIN O/P


SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons FULL JOIN Orders ON Persons.PId=Orders.PId ORDER BY Persons.LastName
LastName Paul Paul Nitty Nitty Samineni
NULL

FirstName Joseph Joseph Carmel Carmel Anthony


NULL

OrderNo 22456 24562 77895 44678 NULL 34764

Vous aimerez peut-être aussi