SQL
Short Description
sql...
Description
Microsoft - MS SQL Oracle - PL SQL ---------------------------------------------------------------------------------------------------------------------------------------------------Data Definition Language (DDL)-CREATE, ALTER, DROP, RENAME, and TRUNCATE. Data Manipulation Language (DML)-SELECT, INSERT, UPDATE, and DELETE. Transaction Control Language (TCL)-COMMIT, ROLLBACK, and SAVEPOINT. Data Control Language (DCL)-GRANT and REVOKE. ---------------------------------------------------------------------------------------------------------------------------------------------------CREATE TABLE table_name(column_name1 datatype, column_name2 datatype, ... column _nameN datatype ); CREATE TABLE Dept(DeptNo NUMBER(2), DeptName VARCHAR2(10) CreateDate DATE DEFAUL T sysdate); CREATE TABLE temp_employee as SELECT * FROM employee ALTER TABLE table_name ADD column_name datatype; ALTER TABLE employee ADD experience number(3); ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE employee DROP COLUMN location; ALTER TABLE table_name MODIFY column_name datatype; ALTER TABLE employee MODIFY salary number(15,2); RENAME old_table_name To new_table_name; RENAME employee TO my_emloyee; DROP TABLE table_name; TRUNCATE TABLE table_name ---------------------------------------------------------------------------------------------------------------------------------------------------SELECT column_list FROM table-name[WHERE Clause][GROUP BY clause][HAVING clause] [ORDER BY clause]; INSERT INTO TABLE_NAME [ (col1, col2, col3,...colN)] VALUES (value1, value2, val ue3,...valueN); INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN); INSERT INTO employee (id, name, dept, age, salary location) SELECT emp_id, emp_n ame, dept, age, salary, location FROM temp_employee; UPDATE table_name SET column_name1 = value1,column_name2 = value2, ...[WHERE con dition] UPDATE employee SET location ='Mysore' WHERE id = 101; UPDATE employee SET job_id =(SELECT job_id FROM emp1 where empid=100), salary =(SELECT salary FROM emp2 where e mpid=200) WHERE empid = 50 DELETE FROM table_name [WHERE condition]; DELETE FROM employee WHERE id = 100; DELETE FROM employee WHERE Deptid = (SELECT deptid FROM dept WHERE DeptName LIKE '%Public%') ---------------------------------------------------------------------------------------------------------------------------------------------------COMMIT: DELETE FROM Dept WHERE DeptID IN (10,20) UPDATE Emp SET DeptID = 80 WHERE empID = 100 COMMIT;
ROLLBACK: DELETE FROM Dept WHERE DeptID IN (10,20) ROLLBACK; SAVEPOINT: UPDATE... SAVEPOINT Update_Done; INSERT... ROLLBACK TO Update_Done; ---------------------------------------------------------------------------------------------------------------------------------------------------GRANT: GRANT SELECT ON employee TO user1; REVOKE: REVOKE SELECT ON employee FROM user1; ROLE: CREATE ROLE Test [IDENTIFIED BY PWD]; DROP ROLE Test; ---------------------------------------------------------------------------------------------------------------------------------------------------LOWER: Lower('SQL Course') = sql course UPPER: Upper('SQL Course') = SQL COURSE INITCAP: InitCap('SQL Course') = SQL Course CONCAT: Concat('Hello','World') = HelloWorld SUBSTR: Substr('HelloWorld'1,5) = Hello LENGTH: Lenght('HelloWorld') = 10 INSTR: Instr('HelloWorld','W') = 6 LPAD: Lpad(Salary,10,'*')=*****24000 RPAD: Rpad(Salary,10,'*')=24000***** REPLACE: Replace('Jack and Jue','J','BL') = Black and Blue TRIM: Trim('H' from 'HelloWorld') = elloWorld ROUND: Round(45.926,2) = 45.93 TRUNC: Trunc(45.926,2) = 45.92 MOD: MOD(1600,300) = 100 MONTHS_BETWEEN: Months_Between('01-SEP-95','11-JAN-94') = 19.677 ADD_MONTHS: Add_Months('11-JAN-94',6) = '11-JUL-94' NEXT_DAY: Next_Day('01-SEP-95','Friday') = '08-SEP-95' LAST_DAY: Last_Day('01-Feb-95') = '28-Feb-95' LIKE: SELECT first_name, last_name FROM student_details WHERE first_na me LIKE 'S%'; BETWEEN AND:SELECT first_name, last_name, age FROM student_details WHERE age BET WEEN 10 AND 15; IN: SELECT first_name, last_name, subject FROM student_detai ls WHERE subject IN ('Maths', 'Science'); ORDER BY: SELECT column-list FROM table_name [WHERE condition][ORDER BY co lumn1 [, column2, .. columnN] [DESC]]; SELECT name, salary FROM employee ORDER BY salary; SELECT name, salary FROM employee ORDER BY 1, 2; COUNT: SELECT COUNT (*) FROM employee WHERE Dept = 'Electronics'; DISTINCT: SELECT DISTINCT Dept FROM employee; SELECT COUNT (DISTINCT name) FROM employee; MAX: SELECT MAX (salary) FROM employee; MIN: SELECT MIN (salary) FROM employee; AVG: SELECT AVG (salary) FROM employee; SUM: SELECT SUM (salary) FROM employee; GROUP BY: SELECT location, Dept, SUM (salary)FROM employee GROUP BY locati on, dept; HAVING: SELECT Dept, SUM (salary)FROM employee GROUP BY dept HAVING SUM (salary) > 25000
ANY:
SELECT EmpID, LastName, JobID, Salary FROM Employees WHERE Salary < ANY (SELECT Salary FROM Employees WHERE J
obID='IT') < ANY means less than Max Salary in return salar ies > ANY means more than Min Salary in return salar ies = ANY means IN SELECT EmpID, LastName, JobID, Salary FROM Employees WHERE Salary < ALL (SELECT Salary FROM Employees WHERE J
ALL: obID='IT')
< ALL means less than Min Salary > ALL means more than Max Salary NVL:
IF Commission NULL, replace value. NVL(Commission,0) NVL(Hire_Date,'01-JAN-97') NVL(Job_ID,'No JOB')
NVL2:
IF Commission NULL, 1stValue else 2ndValue NVL2(Commission,'2ndValue','1stValue')
NULLIF: Compares and if values/expressions are equal, returns NULL. NULLIF(Length(FirstName),Length(LastName)) COALESCE:IF Manager_ID Not NULL use it else use commission and if Commission NUL L use -1. COALESCE(Manager_ID,Commission,-1) ---------------------------------------------------------------------------------------------------------------------------------------------------A]PRIMARY KEY Column level: Column name datatype [CONSTRAINT constraint_name] PRIMARY KEY CREATE TABLE employee( id number(5) PRIMARY KEY, Name char(20),salary number(10),location char(10)); CREATE TABLE employee( id number(5) CONSTRAINT e mp_id_pk PRIMARY KEY,Name char(20),salary number(10),location char(10)); Table level: [CONSTRAINT constraint_name] PRIMARY KEY (column_name1,column_na me2,..) // Multiple columns denotes composite primary key CREATE TABLE employee( id number(5),Name char(20 ),salary number(10),location char(10),CONSTRAINT emp_id_pk PRIMARY KEY (id)); B]FOREIGN KEY or Referential Integrity Column level: [CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(co lumn_name) CREATE TABLE product (product_id number(5) CONSTRAINT pd_id_pk PRIMAR Y KEY product_name char(20), supplier_name char(20), unit_price number(10)); CREATE TABLE order_items (order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY, product_id number(5) CONSTRAINT pd_id_fk REFEREN CES
product(product_id), product_name char(20), supplier_name char(20), unit_price number(10));
CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), mgr_id number(5) REFERENCES employee(id), salary number(10), location char(10)) Table level: [CONSTRAINT constraint_name] FOREIGN KEY(column_name) REFERENCES referenced_table_name(column_name); CREATE TABLE order_items (order_id number(5) , product_id number(5), product_name char(20), supplier_name char(20), unit_price number(10) CONSTRAINT od_id_pk PRIMARY KEY(order_id), CONSTRAINT pd_id_fk FOREIGN KEY(product_id) REFE RENCES product(product_id)); C]NOT NULL [CONSTRAINT constraint name] NOT NULL CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10)); D]UNIQUE KEY Column level: [CONSTRAINT constraint_name] UNIQUE CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20),dept char(10), age number(2), salary number(10), location char(10) UNIQUE); CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT loc_un UNIQUE); Table level:
[CONSTRAINT constraint_name] UNIQUE(column_name) CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10), CONSTRAINT loc_un UNIQUE(location));
E]CHECK CONSTRAINT -
Column level:
[CONSTRAINT constraint_name] CHECK (condition) CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), gender char(1) CHECK (gender in ('M','F')), salary number(10), location char(10));
Table level: CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), gender char(1), salary number(10), location char(10), CONSTRAINT gender_ck CHECK (gender in ('M','F')) ); ---------------------------------------------------------------------------------------------------------------------------------------------------SQL JOINSA]SQL Equi Joins: 1)SQL Inner JoinNATURAL JOIN: SELECT EmpID, EmpName, DeptID, LocationID FROM Emp NATURAL JOIN Dept USING: SELECT EmpID, EmpName, DeptID, LocationID FROM Emp JOIN Dept USING (DeptID) ON: SELECT EmpID, EmpName, DeptID, LocationID FROM Emp E JOIN Dept D ON E.DeptID = D.DeptID -SELECT O.order_id, P.product_name, P.unit_price FROM pr oduct P, order_items O WHERE O.product_id = P.product_id; 2)SQL Outer JoinLeft Outer Join: SELECT e.LastName, d.DeptId, d.DeptName FROM Employee e LEFT OUTER JOIN Department d ON (e.DeptId = d.DeptId) SELECT O.order_id, P.product_name, P.unit_price FROM product P, order_items O WHERE O.product_id(+)= P.product_id; SELECT Persons.LastName, Persons.FirstName, Orde rs.OrderNo FROM Persons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Pe rsons.LastName Right Outer Join: SELECT e.LastName, d.DeptId, d.DeptName FROM Employee e RIGHT OUTER JOIN Department d ON (e.DeptId = d.DeptId) SELECT O.order_id, P.product_name, P.unit_price FROM product P, order_items O WHERE O.product_id = P.product_id(+); SELECT Persons.LastName, Persons.FirstName, Orde
rs.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY P ersons.LastName Full Outer Join: SELECT e.LastName, d.DeptId, d.DeptName FROM Employee e FULL OUTER JOIN Department d ON (e.DeptId = d.DeptId) SELECT Persons.LastName, Persons.FirstName, Orde rs.OrderNo FROM Persons FULL JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Pe rsons.LastName 3)SQL Self JoinSELECT A.sales_person_id, A.name, A.manager_id, B.sales_ person_id, B.name FROM sales_person A, sales_person B WHERE A.manager_id = B.sal es_person_id; B]SQL Non Equi Joins: SELECT O.order_id, P.product_name, P.unit_price FROM product P, order_items O WHERE O.product_id != P.product_id; C]Cross Joins: Returns Cartesian Product (m x n) from 2 tables Select EmpNo, EmpName, Job, DeptNo,DeptName from Emp CROSS JOIN Dept; ---------------------------------------------------------------------------------------------------------------------------------------------------AUTO INCREMENT a Field: CREATE SEQUENCE seq_person MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10 INSERT INTO Persons (P_Id,FirstName,LastName)VALUES (seq_person. nextval,'Lars','Monsen') ---------------------------------------------------------------------------------------------------------------------------------------------------CASE Expression: SELECT LastName, Salary, (CASE WHEN Salary
View more...
Comments