0% found this document useful (0 votes)
2 views

Hsslive-CS-chapt-9-Structured-Query-Language

Structured Query Language (SQL) is a language used for managing data in relational database management systems (RDBMS), allowing users to create, modify, and query tables. It consists of three main components: Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL), each serving different purposes in database management. SQL commands include creating tables, inserting and updating data, and retrieving information using various operators and functions.

Uploaded by

shinasn669
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Hsslive-CS-chapt-9-Structured-Query-Language

Structured Query Language (SQL) is a language used for managing data in relational database management systems (RDBMS), allowing users to create, modify, and query tables. It consists of three main components: Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL), each serving different purposes in database management. SQL commands include creating tables, inserting and updating data, and retrieving information using various operators and functions.

Uploaded by

shinasn669
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Computer Science – XII Chapter 9

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

DROP TABLE DROP TABLE <table_name>; To remove a table from a


database.
INSERT INTO INSERT INTO tbl_name Use of Null as a If a row does not contain
(To insert a VALUES (val1, val2, val3, ... ); value values for all the
record) columns, the keyword
Null should be given for
the respective column.
SELECT SELECT col_name1, col_name2, … DISTINCT column To avoid duplicate values
FROM tbl_name; in the given column while
selecting rows.
(To retrieve (Instead of specifying all columns, WHERE condition; To select only those rows
information the symbol * can be used) (Conditions are which satisfy the given
from table) made using condition.
relational operators)
ORDER BY column To list the selected rows
(Keyword DESC in ascending order of
may be used after values in the specified
column name to column.
get the list in
descending order)
GROUP BY column To group the rows having
(Usually used same value in the
when aggregate specified column. The
function are SELECT command will be
applied) applied on the groups.
HAVING condition To form groups based on
(Used with condition.
GROUP BY clause)
UPDATE UPDATE tbl_name WHERE condition; To modify the columns of
(To modify SET col_name = value; only those rows which
the values in (Value may be a constant or an satisfy the specified
columns) expression) condition.
DELETE DELETE FROM tbl_name; WHERE condition; To delete only those rows
(To delete which satisfy the
rows/records) specified condition.

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

Relational Operators for setting conditions


< <= > >= = <> (Not equal to)
Special Operators & Aggregate Functions
Operators are used to make conditions to attach with WHERE clause.
Functions used with SELECT command to get processed results from tuples.
Operator/
Purpose Example
Function
LIKE To identify pattern matching. SELECT * FROM Student
WHERE Name LIKE “%Kumar”;
BETWEEN To identify value that falls in a SELECT * FROM Employee
... AND given range. WHERE Salary BETWEEN 10000 AND 20000;
IN To identify value from a given SELECT * FROM Bank
list. WHERE Branch IN (“Trivandrum”,
“Ernakulam”, “Kozhikode”);
IS To identify null values in a SELECT * FROM Stock
column. WHERE Tax IS NULL;
AND To select rows when two or SELECT * FROM Student
more conditions are TRUE. WHERE Batch = “Science” AND Marks > 50;
OR To select rows when any one of SELECT * FROM Players
the conditions is TRUE. WHERE Game = “Cricket” OR Game = “Hockey”;
NOT To select rows when the given SELECT * FROM Stock
condition is FALSE. WHERE Tax IS NOT NULL;
Aggregate Functions
COUNT() To count the non-null values of a column. SELECT COUNT(Fee) FROM Student;
Also used to get number of rows SELECT COUNT(*) FROM Student;
SUM() To find the sum of values in a column. SELECT SUM(Fee) FROM Student;
AVG() To find the average of values in a column. SELECT AVG(Salary) FROM Employee;
MAX() To find the highest value in a column. SELECT MAX(Marks) FROM Student;
MIN() To find the lowest value in a column. SELECT MIN(Marks) FROM Student;

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

You might also like