SQL

Data Definition Language (DDL)

 

LIKE operator

 

IS NULL operator

 

START <filename> or @<filename>

 

&

 

RUN

 

_ for single character substitution and % for multi-character substitution

 

True

 

True

 

Insert, update, delete, select, references, index, execute, alter, all

 

REVOKE

 

USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD

 

USER_CONSTRAINTS

 

DELETE FROM EMP;

Will the outputs of the above two commands differ?

Both will result in deleting all the rows in the table EMP.

 

TRUNCATE is a DDL command whereas DELETE is a DML command. Hence DELETE operation can be rolled back, but TRUNCATE operation cannot be rolled back. WHERE clause can be used with DELETE and not with TRUNCATE.

 

Answer :

CREATE TABLE .. AS SELECT command

Explanation :

To copy only the structure, the WHERE clause of the SELECT command should contain a FALSE statement as in the following.

CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;

If the WHERE condition is true, then all the rows or rows satisfying the condition will be copied to the new table.

 

SELECT REPLACE(TRANSLATE(LTRIM(RTRIM('!! ATHEN !!','!'), '!'), 'AN', '**'),'*','TROUBLE') FROM DUAL;

TROUBLETHETROUBLE

 

SELECT DECODE(TRANSLATE('A','1234567890','1111111111'), '1','YES', 'NO' );

Answer :

NO

Explanation :

The query checks whether a given string is a numerical digit.

 

SELECT SAL + NVL(COMM,0) FROM EMP;

This displays the total salary of all employees. The null values in the commission column will be replaced by 0 and added to salary.

 

 

MONTHS_BETWEEN

 

DROP TABLE &TABLE_NAME;

Variable names should start with an alphabet. Here the table name starts with an '&' symbol.

 

The privilege receiver can further grant the privileges he/she has obtained from the owner to any other user.

 

It is used to drop constraints specified on the table.

 

UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;

sal = 11000, comm = 1000

 

Answer :

DESC has two purposes. It is used to describe a schema as well as to retrieve rows from table in descending order.

Explanation :

The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output sorted on ENAME in descending order.

 

When this clause is used with the DROP command, a parent table can be dropped even when a child table exists.

 

FLOOR

 

SELECT TRUNC(1234.5678,-2) FROM DUAL;

1200

 

 

 

 

 

 

SQL – QUERIES

 

I. SCHEMAS

 

Table 1 :STUDIES

 

PNAME (VARCHAR), SPLACE (VARCHAR), COURSE (VARCHAR), CCOST (NUMBER)

 

Table 2 : SOFTWARE

 

PNAME (VARCHAR), TITLE (VARCHAR), DEVIN (VARCHAR), SCOST (NUMBER), DCOST (NUMBER), SOLD (NUMBER)

 

Table 3 : PROGRAMMER

 

PNAME (VARCHAR), DOB (DATE), DOJ (DATE), SEX (CHAR), PROF1 (VARCHAR), PROF2 (VARCHAR), SAL (NUMBER)

 

LEGEND :

 

PNAME – Programmer Name, SPLACE – Study Place, CCOST – Course Cost, DEVIN – Developed in, SCOST – Software Cost, DCOST – Development Cost, PROF1 – Proficiency 1

 

QUERIES :

 

  1. Find out the selling cost average for packages developed in Oracle.
  2. Display the names, ages and experience of all programmers.
  3. Display the names of those who have done the PGDCA course.
  4. What is the highest number of copies sold by a package?
  5. Display the names and date of birth of all programmers born in April.
  6. Display the lowest course fee.
  7. How many programmers have done the DCA course.
  8. How much revenue has been earned through the sale of packages developed in C.
  9. Display the details of software developed by Rakesh.
  10. How many programmers studied at Pentafour.
  11. Display the details of packages whose sales crossed the 5000 mark.
  12. Find out the number of copies which should be sold in order to recover the development cost of each package.
  13. Display the details of packages for which the development cost has been recovered.
  14. What is the price of costliest software developed in VB?
  15. How many packages were developed in Oracle ?
  16. How many programmers studied at PRAGATHI?
  17. How many programmers paid 10000 to 15000 for the course?
  18. What is the average course fee?
  19. Display the details of programmers knowing C.
  20. How many programmers know either C or Pascal?
  21. How many programmers don’t know C and C++?
  22. How old is the oldest male programmer?
  23. What is the average age of female programmers?
  24. Calculate the experience in years for each programmer and display along with their names in descending order.
  25. Who are the programmers who celebrate their birthdays during the current month?
  26. How many female programmers are there?
  27. What are the languages known by the male programmers?
  28. What is the average salary?
  29. How many people draw 5000 to 7500?
  30. Display the details of those who don’t know C, C++ or Pascal.
  31. Display the costliest package developed by each programmer.
  32. Produce the following output for all the male programmers

Programmer

Mr. Arvind – has 15 years of experience

 

KEYS:

 

  1. SELECT AVG(SCOST) FROM SOFTWARE WHERE DEVIN = 'ORACLE';
  2. SELECT PNAME,TRUNC(MONTHS_BETWEEN(SYSDATE,DOB)/12) "AGE", TRUNC(MONTHS_BETWEEN(SYSDATE,DOJ)/12) "EXPERIENCE" FROM PROGRAMMER;
  3. SELECT PNAME FROM STUDIES WHERE COURSE = 'PGDCA';
  4. SELECT MAX(SOLD) FROM SOFTWARE;
  5. SELECT PNAME, DOB FROM PROGRAMMER WHERE DOB LIKE '%APR%';
  6. SELECT MIN(CCOST) FROM STUDIES;
  7. SELECT COUNT(*) FROM STUDIES WHERE COURSE = 'DCA';
  8. SELECT SUM(SCOST*SOLD-DCOST) FROM SOFTWARE GROUP BY DEVIN HAVING DEVIN = 'C';
  9. SELECT * FROM SOFTWARE WHERE PNAME = 'RAKESH';
  10. SELECT * FROM STUDIES WHERE SPLACE = 'PENTAFOUR';
  11. SELECT * FROM SOFTWARE WHERE SCOST*SOLD-DCOST > 5000;
  12. SELECT CEIL(DCOST/SCOST) FROM SOFTWARE;
  13. SELECT * FROM SOFTWARE WHERE SCOST*SOLD >= DCOST;
  14. SELECT MAX(SCOST) FROM SOFTWARE GROUP BY DEVIN HAVING DEVIN = 'VB';
  15. SELECT COUNT(*) FROM SOFTWARE WHERE DEVIN = 'ORACLE';
  16. SELECT COUNT(*) FROM STUDIES WHERE SPLACE = 'PRAGATHI';
  17. SELECT COUNT(*) FROM STUDIES WHERE CCOST BETWEEN 10000 AND 15000;
  18. SELECT AVG(CCOST) FROM STUDIES;
  19. SELECT * FROM PROGRAMMER WHERE PROF1 = 'C' OR PROF2 = 'C';
  20. SELECT * FROM PROGRAMMER WHERE PROF1 IN ('C','PASCAL') OR PROF2 IN ('C','PASCAL');
  21. SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('C','C++') AND PROF2 NOT IN ('C','C++');
  22. SELECT TRUNC(MAX(MONTHS_BETWEEN(SYSDATE,DOB)/12)) FROM PROGRAMMER WHERE SEX = 'M';
  23. SELECT TRUNC(AVG(MONTHS_BETWEEN(SYSDATE,DOB)/12)) FROM PROGRAMMER WHERE SEX = 'F';
  24. SELECT PNAME, TRUNC(MONTHS_BETWEEN(SYSDATE,DOJ)/12) FROM PROGRAMMER ORDER BY PNAME DESC;
  25. SELECT PNAME FROM PROGRAMMER WHERE TO_CHAR(DOB,'MON') = TO_CHAR(SYSDATE,'MON');
  26. SELECT COUNT(*) FROM PROGRAMMER WHERE SEX = 'F';
  27. SELECT DISTINCT(PROF1) FROM PROGRAMMER WHERE SEX = 'M';
  28. SELECT AVG(SAL) FROM PROGRAMMER;
  29. SELECT COUNT(*) FROM PROGRAMMER WHERE SAL BETWEEN 5000 AND 7500;
  30. SELECT * FROM PROGRAMMER WHERE PROF1 NOT IN ('C','C++','PASCAL') AND PROF2 NOT IN ('C','C++','PASCAL');
  31. SELECT PNAME,TITLE,SCOST FROM SOFTWARE WHERE SCOST IN (SELECT MAX(SCOST) FROM SOFTWARE GROUP BY PNAME);

32.SELECT 'Mr.' || PNAME || ' - has ' || TRUNC(MONTHS_BETWEEN(SYSDATE,DOJ)/12) || ' years of experience' “Programmer” FROM PROGRAMMER WHERE SEX = 'M' UNION SELECT 'Ms.' || PNAME || ' - has ' || TRUNC (MONTHS_BETWEEN (SYSDATE,DOJ)/12) || ' years of experience' “Programmer” FROM PROGRAMMER WHERE SEX = 'F';

 

 

 

II . SCHEMA :

 

Table 1 :DEPT

 

DEPTNO (NOT NULL , NUMBER(2)), DNAME (VARCHAR2(14)),

LOC (VARCHAR2(13)

 

Table 2 :EMP

 

EMPNO (NOT NULL , NUMBER(4)), ENAME (VARCHAR2(10)),

JOB (VARCHAR2(9)), MGR (NUMBER(4)), HIREDATE (DATE),

SAL (NUMBER(7,2)), COMM (NUMBER(7,2)), DEPTNO (NUMBER(2))

 

MGR is the empno of the employee whom the employee reports to. DEPTNO is a foreign key.

QUERIES

 

 

KEYS: