Saturday, 24 May 2014

SQL Query


SQL Query to find second highest salary of Employee

select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );



SQL Query to find Max Salary from each department.


SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.



Write a SQL Query to print the name of distinct  employee whose DOB is between 01/01/1960 to 31/12/1975.

SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN ‘01/01/1960’ AND ‘31/12/1975’;




Write an SQL Query to find employee whose Salary is equal or greater than 10000.

SELECT EmpName FROM Employees WHERE Salary>=10000;



Write an SQL Query to find name of employee whose name Start with ‘M’

SELECT * FROM Employees WHERE EmpName like 'M%';





Find all Employee records containing the word "Joe", regardless of whether it was stored as JOE, Joe, or
joe.


SELECT * from Employees WHERE upper(EmpName) like upper('joe%');





Finding nth highest salary example

SELECT *
FROM Employee Emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2

WHERE Emp2.Salary > Emp1.Salary)


Delete multiple duplicate rows ?
OR
How to Delete Duplicate Records in Oracle ?



DELETE FROM your_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM your_table
GROUP BY column1, column2, column3);


delete from emp a where rowid != (select max(rowid) from emp b where  a.empno=b.empno);


Sunday, 18 May 2014

Explain the use of the Join keyword and its various types.


The join keyword is very powerful in SQL. It can be used to combine rows from multiple tables by using
common values in certain fields. The type of join decides which rows are to be selected, while the
select statement specifies which fields to include in the combination table.


Inner Join
This is the default type of join. It picks all rows that have matching fields, or in other words, that meet the join condition.


Outer Join
A right outer join picks all rows from the table on the right, even if they do not meet the join condition. Some fields in such rows may have null values in the resulting table.A left outer join returns all rows of the left-side
table, irrespective of their match with the right-side table.


A full outer join returns all rows of the left- and
right-side tables.
Self Join

This is a special type of join where a table joins to
itself.

Cross Join
This is the Cartesian product of rows from the tables included in the join query statement. In other words,
every row from the first table is combined with every row of the second table, one at a time.


What is the SQL syntax for sorting, and which is the default order?


The default sorting order is ascending. These two statements are identical:
select from order by
select from order by asc
For descending order, simply replace “asc” with “desc.”

Wednesday, 14 May 2014

SQL is mainly divided into 4 sub languages


1. Data Definition Language (DDL).These commands are auto commit.
2. Data Manipulation Language (DML).
3. Transaction Control Language (TCL).
4. Data Control Language (DCL).


DDL(  DR CAT)
DML (SUDI)
TCL (CRS)
DCL (GR)
1.DROP
1.SELECT
1.COMMIT
1.GRANT
2.RENAME
2.UPDATE
2.ROLLBACK
2.RENAME
3.CREATE
3.DELETE
3.SAVEPOINT

4.ALTER
4.INSERT


5.TRUNCATE





There are 10 types  Database Objects in Oracle
6 in SQL and 4 in PL/SQL


6 Sql Database Object
1.TABLE
2.VIEWS
3.SYNONYMS
4.SEQUENCE
5.INDEX
6.CLUSTER


4  DATABASE OBJECT IN PL/SQL
1.FUNCTION
2.PROCEDURE
3.PACKAGE
4.TRIGGER