AHS SQL Server
AHS SQL Server
Categorization of Commands Commands with Examples Joins Types of Joins with examples FAQ s
MS - SQL SERVER
www.huconsolutions.com
Categorization of Commands
Data Manipulation Language Data Definition Language Data Control Language Transaction Control Language
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
Ex(diff Formats):
Emp
Note :
Table names, Column names, keywords are case insensitive.
Ex(2 formats):
Insert into emp values(5, Swamy ,7000) Insert into emp(eid,ename,sal) values(5, Swamy ,7000)
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
Where, Between , AND, OR , IN , UNION , UNION ALL , INTERSECT , ANY, LIKE, TOP, Group by, Order by, Having < , > , = , <> , <= , >=
<>
Not Equal, in
some versions it is !=
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
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]
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
Eid Ename
Sal Emp
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)
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
Table Structure
Products
City Hyderabd
Paul
Joseph
Hitech City
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
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
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
FULL JOIN
The FULL JOIN keyword return rows when there is a match in one of the tables.