Hsslive CS Chapt 9 Structured Query Language
Hsslive CS Chapt 9 Structured Query Language
Chapter 9
Structured Query Language
Structured Query Language (SQL) is a language designed for managing data in RDBMS. It
provides facilities to create a table, insert data into a table, retrieve information from a
table, modify data in the table, delete the existing data from a table, modify the structure of
a table, remove a table from a database, etc.
Components of SQL: Data Definition Language (DDL), Data Manipulation language (DML)
and Data Control Language (DCL).
DDL commands are used to create, modify and remove the database objects such as tables,
views and keys. Eg: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW, DROP VIEW.
DML permits users to insert data into tables, retrieve existing data, delete data from tables
and modify the stored data. Eg: INSERT INTO, SELECT, UPDATE, DELETE FROM.
DCL includes commands that control a database, including administering privileges and
committing data. Eg: GRANT, REVOKE.
SQL Data Types: INT or INTEGER, DEC or DECIMAL, CHAR or CHARACTER, VARCHAR, DATE, TIME.
DEC(5,2) or DECIMAL(5,2) denotes that the column with this specification can store any value
having a maximum of five digits, out of which two are after the decimal point.
CHAR is a fixed length character data type. It is mainly used when the data in a column are of
the same fixed length and small in size. VARCHAR represents variable length strings. The
space allocated for the data depends only on the actual size of the string, not on the
declared size of the column.
SQL Commands
Command
Syntax Optional Purpose
and use
CREATE TABLE CREATE TABLE tbl_name Constraints – The
(To create a (col_name data type <constraint>, rules enforced on
table) col_name data type <constraint>, data that are
……………………… entered into the
col_name data type <constraint>); column of a table. 1. To uniquely identify a
1. PRIMARY KEY row of a table.
2. AUTO_INCREMENT 2. To assign serial
3. NOT NULL numbers automatically.
4. UNIQUE 3. To avoid null value.
4. To avoid duplication.
5. DEFAULT
5. To set a default value.
ALTER TABLE ALTER TABLE tbl_name DROP column Instead of ADD / MODIFY
(To change ADD / MODIFY use DROP to remove an
the structure col_name data type <constraint>; existing column.
of a table) (To add a new column or modify RENAME TO To change the name of a
an existing column in a table) new_tbl_name; table.
1
Joy John’s CS capsules
Computer Science – XII Chapter 9
Additional Commands
Command Purpose
CREATE DATABASE db_name; To create a new database.
USE db_name; To open a database to perform operation on tables.
DESCRIBE tbl_name; To display the structure of a table.
SHOW TABLES; To list the tables in the current database.
2
Joy John’s CS capsules
Computer Science – XII Chapter 9
Nested Query
When we use SELECT command with WHERE clause, the condition may be framed with
another SELECT query.
For example, the following statement gives the details of students who got highest marks:
SELECT * FROM Student
WHERE Marks = (SELECT MAX(Marks) FROM Student);
The following statement gives the details of employees who get lowest salary:
SELECT * FROM Employee
WHERE Salary = (SELECT MIN(Salary) FROM Employee);
View: It is a virtual table that does not really exist in the database, but is derived from one
or more tables. A view can be created with the DDL command CREATE VIEW.
CREATE VIEW <view_name>
AS SELECT <columns> FROM <table_name>
3
Joy John’s CS capsules
Computer Science – XII Chapter 9
[WHERE <condition>];
4
Joy John’s CS capsules
Computer Science – XII Chapter 9
(ii) Display Acc. No., Branch and Amount in the descending order of amount.
(iii) Display the number of accounts in each branch. (3) (March 2016)
(b) Write SQL statements to do the following:
(i) Add a new record into the table.
(ii) Update the amount of Sanjay to 100000.
(iii) Delete the details of Anil. (3) (March 2016)
3. How will you add a new column to an existing table using SQL statement?
(2) (March 2016)
4. ____ clause of SELECT query is used to apply condition to form group of records.
(a) order by (b) group by (c) having (d) where (1) (SAY 2016)
5. What is a view? How can we create a view using SQL statement? (2) (SAY 2016)
6. (a) Explain the SQL statements used to insert and delete data from a table.
(3) (SAY 2016)
(b) Explain any two DDL commands. (3) (SAY 2016)
7. ____ is an SQL data type which is used to represent variable length string.
(1) (March 2017)
8. The structure of the table ‘EMPLOYEE’ is given below:
Empcode Numeric
Empname String
Basicpay Numeric
DA Numeric
Grosspay Numeric
Write SQL statements for the following:
(a) Insert a record into the table.
(b) Update DA with 60% basic pay.
(c) Display the details of employees whose basic pay is greater than 20000.
(d) Rename the table EMPLOYEE to EMPDETAILS. (5) (March 2017)
9. ____ command in SQL is used to display the structure of a table.
(a) LIST (b) STRUCT (c) DESCRIBE (d) SHOW (1) (SAY 2017)
10. Explain primary key constraint with an example. (2) (SAY 2017)
11. Write SQL for:
(a) Create a table student with the data [name char(20), rollno number(3), marks
number(3)].
(b) List name and rollno of all students.
(c) List name and rollno of students having marks > 600. (3) (SAY 2017)
12. An employee table contains name, empno, basicpay, design.
Write SQL for
(a) Display name, empno and basicpay of all managers. (design = “manager”)
(b) Display empno and salary of all employees.
(salary = basicpay + da) (da = basicapy * 1.15)
(c) Display name and empno of all the employees whose basicpay < 10000.
(3) (SAY 2017)
5
Joy John’s CS capsules