SQL(STRUCTURED QUERY LANGUAGE) COMMANDS – COMPUTER SCIENCE 2010-11 COMMAND TO CREATE TABLE EMP: CREATE TABLE EMP

( EMPNO NUMERIC(4) PRIMARY KEY, ENAME VARCHAR(25) NOT NULL, GENDER CHAR(1) NOT NULL, DOB DATE NOT NULL, DEPTNO NUMERIC(2), SAL NUMERIC(8,2), COMM NUMERIC(7,2), JOB VARCHAR(15) ); NOTE: The data type CHAR represents a fixed length allocation for the field or column value. The date type VARCHAR (in some RDBMS equivalents, VARCHAR2 is used) represents variable length storage. The data type DATA is used to store date values. The data type NUMERIC can be substituted by DECIMAL (in some RDBMS, equivalent NUMBER is used). NUMERIC(x,y) represents the total no. of digits including decimal point and the digits after the decimal point respectively. PRIMARY KEY indicates that the date is unique and not null. Composite Key is a combination of two or more Primary Keys. RDBMS: Relational Database Management System TUPLES TO BE INSERTED IN EMP TABLE: INSERT INTO EMP VALUES(101, ‘SANDERS’, ‘M’, ‘1978-05-12’, 10, 18000, NULL, ‘EXECUTIVE’); INSERT INTO EMP VALUES(105, ‘JANE’, ‘F’, ‘1981-10-23’, 20, 15000, NULL, ‘CLERK’); INSERT INTO EMP VALUES(103, ‘BARRY’, ‘M’, ‘1977-02-20’, 30, 10000, NULL, ‘SALESMAN’); INSERT INTO EMP VALUES(104, ‘HOLT’, ‘M’, ‘1975-11-06’, 20, 20000, NULL, ‘ANALYST’); COMMAND TO CREATE TABLE DEPT: CREATE TABLE DEPT ( DEPTNO NUMERIC(2), DEPTNAME VARCHAR(20), LOCATION VARCHAR(25) ); TUPLES TO BE INSERTED IN DEPT TABLE: INSERT INTO DEPT VALUES(10, ‘ADMIN’, ‘CHENNAI’); INSERT INTO DEPT VALUES(20, ‘ACCOUNTS’, ‘MUMBAI’); INSERT INTO DEPT VALUES(30, ‘SALES’, ‘DELHI’); INSERT SQL COMMAND: Version 1 (Mentioning Columns): INSERT INTO EMP (EMPNO, ENAME, GENDER, DOB, DEPTNO, JOB, SAL, COMM) VALUES(103, ‘BARRY’, ‘M’, ‘1977-02-20’, 30, ‘SALESMAN’, 10000, NULL); Version 2 (Without Mentioning Columns): INSERT INTO EMP VALUES(104, ‘HOLT’, ‘M’, ‘1975-11-06’, 20, 20000, NULL, ‘ANALYST’); SELECT COMMAND TO VIEW ENTIRE COMMANDS OF TABLE:

DEPTNO. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING PATTERN MATCHING v1: SELECT * FROM EMP //Details of employees whose names begin with ‘B’ are WHERE ENAME LIKE ‘B%’. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING PATTERN MATCHING v4: SELECT * FROM EMP //Details of employees having ‘R’ as their 3rd letter WHERE ENAME LIKE ‘__R’.SELECT * FROM EMP. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING PATTERN MATCHING v3: SELECT * FROM EMP //Details of employees whose names contain ‘R’ are selected WHERE ENAME LIKE ‘%R%’. SAL FROM EMP WHERE DEPTNO = 20. . ‘_’ symbol can be replaced by any 1 character. ENAME.(2 underscore characters) NOTE: ‘%’ symbol can be replaced by 0 or more characters. ENAME. SELECT COMMAND TO VIEW CONTENTS UNDER SPECIFIED COLUMNS OF TABLE: SELECT EMPNO. SAL FROM EMP. Version 2: Using BETWEEN Operator SELECT * FROM EMP WHERE SAL BETWEEN 12000 AND 20000. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING RANGE: Version 1: Using Relational & Logical Operators SELECT * FROM EMP WHERE SAL >= 12000 AND SAL <= 20000. JOB. DEPTNO. REMOVE DUPLICATE PROJECTIONS (USING DISTINCT SELECT COMMAND TO VIEW CONTENTS OF SPECIFIED ROWS/TUPLES OF THE TABLE: SELECT * FROM EMP WHERE DEPTNO = 20. JOB. // selected SELECT COMMAND TO VIEW CONTENTS OF TABLE USING PATTERN MATCHING v2: SELECT * FROM EMP //Details of employees whose names end with ‘E’ are selected WHERE ENAME LIKE ‘%E”. SELECT COMMAND TO KEYWORD): SELECT DISTINCT JOB FROM EMP. SELECT COMMAND TO VIEW CONTENTS UNDER SPECIFIED COLUMNS OF SPECIFIED ROWS/TUPLES OF THE TABLE: SELECT EMPNO. LIKE VERB is used in pattern matching command. SELECT COMMAN TO VIEW DUPLICATE PROJECTIONS (USING ALL KEYWORD): SELECT ALL JOB FROM EMP.

MAX AGGREGATE FUNCTION: SELECT MAX(SAL) AS “MAXIMUM SALARY” FROM EMP . SORTING PROJECTIONS: SELECT EMPNO. SAL DESC. ENAME. AVG AGGREGATE FUNCTION: SELECT AVG(SAL) AS “AVERAGE SALARY” FROM EMP WHERE DEPTNO = 20. UPDATE COMMAND (SPECIFIED ROWS): UPDATE EMP SET COMM = SAL * 0. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING NOT EQUAL TO OPERATOR: Version 1 SELECT * FROM EMP WHERE DEPTNO <> 10. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING OR OPERATOR: SELECT * FROM EMP WHERE DEPTNO = 10 OR DEPTNO = 30. UPDATE COMMAND (SPECIFIED COLUMNS OF SPECIFIED ROWS): UPDATE EMP SET SAL = SAL * 1.30).1 WHERE JOB = ‘SALESMAN’. COMM = COMM + (SAL * 0. Version 2 SELECT * FROM EMP WHERE DEPTNO != 10.SELECT COMMAND TO VIEW CONTENTS OF TABLE USING IN OPERATOR: SELECT * FROM EMP WHERE DEPTNOIN (10.05. UPDATE COMMAND (ALL ROWS/TUPLES): UPDATE EMP SET SAL = SAL * 1. SELECT COMMAND TO VIEW CONTENTS OF TABLE USING NOT OPERATOR: SELECT * FROM EMP WHERE NOT DEPTNO = 10. NOTE: ASC (Default Mode) – Ascending Order Sorting. DELETE COMMAND (SPECIFIED ROWS): DELETE FROM EMP WHERE DEPTNO = 40.05) WHER JOB = ‘SALESMAN’. DEPTNO.1. DELETE COMMAND (ALL ROWS – DANGEROUS!!!): DELETE FROM EMP. DESC – Descending Order Sorting AGGREGATE FUNCTIONS (FUNCTIONS THAT ACT ACROSS ROWS): SUM AGGREGATE FUNCTION: SELECT SUM(SAL) AS “TOTAL SALARY” FROM EMP WHERE DEPTNO = 20. SAL FROM EMP ORDER BY DEPTNO ASC.

DEPT WHERE EMP.DEPTNO = B.WHERE DEPTNO = 20. TO ADD A NEW COLUMN TO TABLE: ALTER TABLE EMP ADD DUMMY CHAR(1).DEPTNAME FROM EMP. TO ACCESS DATA FROM ANOTHER TABLE USING EQUI-JOIN v2: SELECT A. OF EMPLOYEES” FROM EMP WHERE DEPTNO = 20. COUNT AGGREGATE FUNCTION v2: SELECT COUNT(*) AS “NO.SAL.SAL. COUNT AGGREGATE FUNCTION v3: SELECT COUNT(COMM) AS “NO.DEPTNO. COUNT AGGREGATE FUNCTION v1: SELECT COUNT(EMPNO) AS “NO.EMPNO.//DEPTNO = 20 is conditional expression which evaluates to TRUE. DEPT. COUNT AGGREGATE FUNCTION v4: SELECT COUNT(DISTINCT JOB) FROM EMP.ENAME. TO DROP A TABLE ITSELF (DANGEROUS!!): DROP TABLE EMP. FALSE or NULL. SUM(SAL) “TOTAL SALARY” FROM EMP GROUP BY DEPTNO HAVING SUM(SAL) > 35000. WHERE DEPTNO = 20 is the conditional clause. GROUP BY HAVING COMMAND: SELECT DEPTNO “DEPARTMENT”. SUM(SAL) “TOTAL SALARY” FROM EMP GROUP BY DEPTNO.ENAME. A. WHERE A. A. B. TO DROP A COLUMN FROM A TABLE: ALTER TABLE EMP DROP DUMMY. A. MIN AGGREGATE FUNCTION: SELECT MIN(SAL) AS “MINIMUM SALARY” FROM EMP WHERE DEPTNO = 20. . GROUP BY COMMAND: SELECT DEPTNO “DEPARTMENT”.JOB.DEPTNO. EMP.JOB.DEPTNAME FROM EMP A.EMPNO. TO ACCESS DATA FROM ANOTHER TABLE USING EQUI-JOIN v1: SELECT EMP. EMP. OF EMPLOYEES” FROM EMP HERE DEPTNO = 20. EMP. OF EMPLOYEES” FROM EMP WHERE DEPTNO = 20. DEPT B.DEPTNO = DEPT.

Sign up to vote on this title
UsefulNot useful