RDBMS
UNIT – III
STRUCTURE QUERY LANGUAGE (SQL)
Q1) What is SQL ? Write the different data types available in SQL.
Ans) E.F.Codd developed the relational model of data base in 1970 . Basing on this relational
model Raymond.F.Boyce developed the SQL language at IBM . SQL is a Database Language
used in RDBMS platform.
Presently SQL is owned by ORACLE CORPORATION,USA.
Different versions of Oracle are released such as Oracle 10, Oracle 11 etc.
Different data types of SQL.
Ans) Data type refers to the type of that a field is storing. For example NUMBER,TEXT,DATE
etc. SQL provides the following data types :
DATA TYPE DESCRIPTION
NUMBER Stores both integer and decimal numbers
VARCHAR2 Stores text data
DATE Stores date type of data
CHAR Store data having one character only
BOOLEAN Stores Boolean value (T/F) or (Y/N)
Q 2) Define SQL commands.
Ans) SQL commands are used to communicate with the database to perform specific task.
SQL commands are divided into four categories:
1. DDL – DATA DEFINITION LANGUAGE (Used to create and modify table)
2. DML- DATA MANIPULATION LANGUAGE(Used to insert, delete, update etc)
3. DCL – DATA CONTROL LANGUAGE( Used to grant & revoke permissions)
4. TCL – TRANSACTION CONTROL LANGUAGE(Used to commit or rollback a
transaction )
1
Q3) Explain DDL command in detail .
DDL - These SQL commands are used to create, alter, drop and truncate the table structure.
The commands are : CREATE ,ALTER,DROP,TRUNCATE
CREATE This command is used to create a new table
SYNTAX : CREATE TABLE <TABLE NAME>
(COL NAME1 DATA TYPE SIZE, COL NAME2 DATA TYPE SIZE );
EXAMPLE:
CREATE TABLE EMP (ENO NUMBER(4), ENAME VARCHAR2(30)) ;
ALTER This command is used to change the structure of a table or a column
SYNTAX :
ALTER TABLE <TABLE NAME> ADD (COL NAME DATA TYPE SIZE)
EXAMPLE:
ALTER TABLE EMP ADD (PF_NO NUMBER(12));
SYNTAX :
ALTER TABLE <TABLE NAME> MODIFY (COL NAME DATA TYPE SIZE)
EXAMPLE:
ALTER TABLE EMP MODIFY (ENAME VARCHAR2(25));
DROP This command is used to delete a table
SYNTAX :
DROP TABLE <TABLE NAME>
EXAMPLE:
DROP TABLE EMP ;
2
TRUNCATE This command is used to remove all the records of a table
SYNTAX :
TRUNCATE TABLE <TABLE NAME>
EXAMPLE:
TRUNCATE TABLE EMP ;
Q 4) Explain DML commands of SQL
Ans) DML commands are used to Insert. Delete, Update and Select records from the table.
INSERT This command is used to insert records in a table
SYNTAX :
INSERT INTO <TABLE NAME> VALUES (VALUE1,VALUE2…….);
EXAMPLE :
INSERT INTO EMP VALUES (101,’JOHN’,8000);
SELECT This command is used to retrieve data from the table
SYNTAX :
SELECT <COL NAMES> FROM <TABLE NAME> <CRITERIA>;
EXAMPLE :
SELECT ENO,ENAME,BS FROM EMP WHERE DEPT = ‘SALES’ ;
UPDATE This command is used to update data
SYNTAX :
UPDATE <TABLE NAME> SET COLNAME = VALUE <CRITERIA>;
EXAMPLE :
UPDATTE EMP SET BS = BS + 1000 WHERE ENO = 108 ;
3
DELETE This command is used to delete records
SYNTAX :
DELETE FROM <TABLE NAME> <CRITERIA>;
EXAMPLE :
DELETE FROM EMP WHERE ENO = 108 ;
Q 5) Explain DCL commands of SQL
DCL These commands are used to control the database . Giving different grants and
permissions to the user comes in this category. Generally these are used in Network
environment.
GRANT & REVOKE comes under DCL
GRANT This command is used to grant privileges to users
SYNTAX :
GRANT <COMMAND> ON < TABLE NAME > TO <USERNAME>;
EXAMPLE-1 :
GRANT CREATE ON EMP TO TO USER5
EXAMPLE-2:
GRANT INSERT,DELETE ON EMP TO USER10
EXAMPLE-3
GRANT ALL ON EMP TO USER1
REVOKE This command is used to denie a given permission to a user
SYNTAX :
REVOKE <COMMAND> ON < TABLE NAME > FROM <USERNAME>;
EXAMPLE :
REVOKE DELEETE ON EMP FROM USER5
4
Q 6) Explain TCL command so SQL
Ans) TCL – These commands are used to control transactions in database
COMMIT, ROLLBACK ,SAVE POINT comes under this category
COMMIT : This command is used to save data permanently in database
SYNTAX:
COMMIT
ROLLBACK: This command is used to restore the database to last committed state.
SYNTAX:
ROLLBACK
SAVEPOINT: This command is used to create a point to roll back to that point in future.
SYNTAX:
SAVEPOINT Y
Q 7) Write about different operators of SQL.
Ans) An operator manipulates individual data items and returns a result. The data items are
called as operands.
SQL provides three types of operatiors:
. 1. ARITHMETIC OPERATOR
2. RELATIONAL OPERATOR
3. LOGICAL OPERATOR
ARITHMETIC RELATIONAL LOGICAL
+ (PLUS) > AND
- (MINUS) < OR
* (ASTERIX) =
/ (SLASH) >=
% (MOD) <=
<>NOT EQUAL TO
5
Q8) Write about Aggreegate functions of SQL.
Ans) A function is a pre-defined formula. A function always returns a value.
SQL AGGREGATE functions are used to manipulate numbers.
Some of the AGGREGATE functions of SQL are:
1.SUM( ) - Returns total of a range of values Example : SUM(SALARY)
2.AVG( ) - Returns average of values Example : AVG(SALARY)
3.MAX( )- Returns maximum value out of a given range. Example MAX(SALARY)
4.MIN( ) - Returns minimum value out of a given range .Example MIN(SALARY)
5.COUNT( ) – Return the number of rows of a table Example :COUNT(NAME)
6.ROUND( ) - Returns rounded value. Example : ROUND(PRICE,2)
Q 9) Write about String functions of SQL .
Ans) A function is a pre-defined formula. A function always returns a value.
SQL String functions are used to manipulate text data
Some of the STRING functions of SQL are :
1.UPPER( ) - Returns upper case letters Example : UPPER(NAME)
2.LOWER( ) – Returns lower case letters . Example : LOWER(NAME)
3.INITCAP( ) - Returns first letter as capital . Example : INITCAP(NAME)
5.LENGTH( ) – Returns length of the text . Example: LENGTH(NAME)
6
Q 10) Explain joins . Write about different types of joins in SQL.
Ans) In SQL joins are used to combine tables. In join columns are two tables are joined with a
common key.
SQL provides four types of joins:
1. INNER JOIN
2.LEFT JOIN
3.RIGHT JOIN
4.FULL JOIN
INNER JOIN : This is called as a simple join. Inner join returns all the rows of two tables
where the joint conditions are met.
EXAMPLE:
SQL> SELECT * FROM STATE INNER JOIN CITY
WHERE STATE.STATE_ID = CITY.STATE_ID
NOTE: Retrieves data from both tables where the condition is satisfied.
LEFT JOIN: Returns all the rows of left tables and only rows of right table which meet the
Condition.
7
.
EXAMPE:
SQL> SELECT * FROM STATE LEFT JOIN CITY
WHERE STATE.STATE_ID = CITY.STATE_ID ;
NOTE: Retrieves all the records of left table STATE and only the matching records of
CITY table
RIGHT JOIN : Returns all the rows of right table and only rows of left side table which
meets the condition.
EXAMPE:
SQL> SELECT * FROM STATE RIGHT JOIN CITY
WHERE STATE.STATE_ID = CITY.STATE_ID ;
NOTE: Retrieves all the records of right table CITY and only the matching records of
STATE table
8
FULL JOIN : Returns all the rows of both the table and inserting null values where the
condition is not met.
EXAMPE:
SQL> SELECT * FROM STATE FULL JOIN CITY
WHERE STATE.STATE_ID = CITY.STATE_ID
NOTE: Retrieves all the records of both STATE & CITY tables irrespective of the
condition.
Q 11) Explain view table. Write different operations performed on view table.
Ans) A view table is not stored in memory . It is not physicaly present. A view is created to
display some of the fields of the table or to see the changes in a table if anything is modified or
updated. This is a virtual or imaginary table only for the display purpose. A view can be created
from one or more tables. To create a view table the user must have the appropriate system
privilege .
Views are of two types:
View read only 2.View updatable
9
CREATE VIEW
SYNTAX: CREATE VIEW <VIEW_NAME > AS
SELECT <COL-1,COL-2……. > FROM <TABLE_NAME> WHERE
{CONDITION} ;
EXAMPLE
SQL> CREATE VIEW VIEW_EMP AS
SELECT E_CODE, E_NAME,BS,DA,HRA FROM EMP
where BS > 15000 ;
UPDATE VIEW
SQL > UPDATE VIEW_EMP SET BS = BS + 1000 WHERE E_CODE = 1003;
DELETE VIEW
SQL> DELETE FROM VIEW_EMP WHERE E_CODE = 1002;
---------END----------
10