SQL
SQL
SQL
-------------------Inner Join-----------------
Select
S_FirstName,S_LastName,E_FirstName,E_LastName,S_Marks,S_Department,E_Salary
from Students
inner join Emp
on S_ID=E_ID
Order by S_LastName
-------------------Left Join-----------------
Select
S_FirstName,S_LastName,E_FirstName,E_LastName,S_Marks,S_Department,E_Salary
from Students
Left join Emp
on S_ID=E_ID
Order by S_LastName
-------------------Right Join-----------------
Select
S_FirstName,S_LastName,E_FirstName,E_LastName,S_Marks,S_Department,E_Salary
from Students
Right join Emp
on S_ID=E_ID
Order by S_LastName
Order by S_LastName
-----------------CROSS JOIN--------------------
This will return all records where each row from the first table is combined
with each row from the second table. A CROSS JOIN can be specified in two
ways: using the JOIN syntax or by listing the tables in the FROM clause
separated by commas without using a WHERE clause to supply join criteria.
Exmp:
---------Emp Table------------------
DELETE users WHER RowID NOT IN (SELECT MIN(RowID) FROM users GROUP BY name,
email);
Or
DELETE FROM tblemp WHERE id IN(SELECT MIN(id) FROM tbemp GROUP BY title HAVING
COUNT(id)>1)
------------DELETE------------
The DELETE command is used to remove rows from a table. A WHERE clause can be
used to only remove some rows. If no WHERE condition is specified, all rows will be
removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the
transaction to make the change permanent or to undo it. Note that this operation will cause
all DELETE triggers on the table to fire.
---------------TRUNCATE--------------
TRUNCATE removes all rows from a table. The operation cannot be rolled back and no
triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a
DELETE
------------------DROP---------------------
The DROP command removes a table from the database. All the tables' rows, indexes
and privileges will also be removed. No DML triggers will be fired. The operation cannot be
rolled back.
The GROUP BY statement is often used with aggregate functions (COUNT, MAX,
MIN, SUM, AVG) to group the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
EX:
---------- Group By --------------------
SELECT COUNT(S_ID),S_FirstName
FROM Students
GROUP BY S_FirstName
1 Amar
1 Harikrishna
1 Hasan
1 Mounica
1 Prateek
1 Sathish
1 Venu
Inside a table, a column often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.
Ex:
The WHERE clause can be combined with AND, OR, and NOT operators.
-----------------AND Syntax----------------
SELECT * From Students,EMP
WHERE S_FirstName = 'Harikrishna' AND E_LastName = 'Challa';
-----------------------OR Syntax--------------
SELECT * From Students,EMP
WHERE S_FirstName = 'Harikrishna' OR E_LastName = 'Challa';
---------------------NOT Syntax-------------------
SELECT * From Students
WHERE NOT S_FirstName = 'Harikrishna'
The SELECT TOP clause is used to specify the number of records to return.
---MAX() Example:
SELECT MAX(S_Marks) AS LargestPrice
FROM Students;
select * from Emp where E_salary in (select distinct top(3) E_salary from
Emp order by E_salary desc)
or
Select MAX(E_salary) from Emp
-----COUNT() Syntax
SELECT COUNT(S_Marks)
FROM Students
-----AVG() Syntax
SELECT AVG(S_Marks)
FROM Students;
-----SUM() Syntax
SELECT SUM(S_Marks)
FROM Students;
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.
There are two wildcards used in conjunction with the LIKE operator:
--------BETWEEN Example----------
SELECT * FROM Students
1000 Index can be used as maximum number per table. 1 Clustered Index and 999 Non-
clustered indexes per table can be used in SQL Server.
What is the difference between SUBSTR and INSTR in the
SQL Server?
The SUBSTR function is used to return specific portion of string in a given string. But,
INSTR function gives character position in a given specified string.
1 SUBSTR(“Smiley”,3)
1 INSTR(“Smiley”,’i’,1)
Column1
10000
12000
9000
13000
14000
8000
18000
48000