SQL Notes
SQL Notes
6 SQL
QUICK REVIEW
ra
CREATE CREATE T A B L E Create a new table with CREATE TABLE employee
tablename (col1 type, 2 columns (id INT PRIMARY KEY,
col2 type…) na me V AR CH AR NO T
ith NULL);
ALTER ALTER TABLE table_ ALTER TABLE lets you Alter table employee
name add columns to a table in ad d c ol um n d es ig
ADD c o l u m n _ n a m e a database. varchar(20)
datatype; Drop column c from the ALTER TABLE employee
ALTER TABLE table_ table DROP COLUMN desig;
v
name DROP COLUMN c; Change the name or type Alter table employee
ALTER TABLE table_ of an existing column modify name ename
Pa
INSERT INSERT INTO t Insert one row into a table INSERT INTO employee
(column_list) VALUES VALUES(333, ‘xxx’,
(value_list) ‘SUPERVISOR’);
UPDATE UPDATE t SET c1 = Update new value in the UPDATE employee SET
new_value; column c1 for the entire desig = “TRAINEE”;
U P D A T E t S E T c 1 table UPDATE employee SET
= new_value WHERE Up da t e v al ues i n the desig = “TRAINEE”
condition; column c1, c2 that match WHERE id=222;
the condition
DELETE DELETE FROM table_ Delete all data in a table DELETE FROM employee;
name Delete subset of rows in DELETE FROM employee
DELETE FROM table_ a table WHERE id=111;
name WHERE condition
62 A PRACTICAL WORKBOOK FOR CBSE COMPUTER SCIENCE (PYTHON)–XII
Aggregate Functions
A multiple row function works on multiple values. These functions are called aggregate functions
or group functions. These functions are:
S. No.
1 MAX() Returns the MAXIMUM
2 MIN() Returns the MINIMUM
ra
3 AVG() Returns the AVERAGE
4 SUM() Returns the SUM
5 COUNT() Returns the COUNT
ith
GROUP BY: GROUP BY clause is used in a SELECT statement in conjunction with aggregate
functions to group the result based on distinct values in a column.
HAVING: HAVING clause is used in conjuction with GROUP BY clause in a SELECT statement
to put condition on groups.
WHERE Vs HAVING: WHERE is used to put a condition on individual row of a table whereas
v
HAVING is used to put condition on individual group formed by GROUP BY clause in a SELECT
statement.
Pa
Examples:
If we want the number of employees in a particular department, the query would be:
SELECT COUNT (*) FROM employee
WHERE dept = ‘Electronics’;
The output would be ‘2’ rows.
If we want the total number of employees in all the department, the query would take the form:
SELECT COUNT (*) FROM employee;
The output would be ‘5’ rows.
This function is used to select the distinct rows.
For Example: If we want to select all distinct department names from employee table, the
query would be:
SELECT DISTINCT dept FROM employee;
To get the count of employees with unique name, the query would be:
SELECT COUNT (DISTINCT name) FROM employee;
This function is used to get the maximum value from a column.
SQL 63
To get the maximum salary drawn by an employee, the query would be:
SELECT MAX (salary) FROM employee;
This function is used to get the minimum value from a column.
To get the minimum salary drawn by an employee, the query would be:
SELECT MIN (salary) FROM employee;
This function is used to get the average value of a numeric column.
To get the average salary, the query would be
SELECT AVG (salary) FROM employee;
This function is used to get the sum of a numeric column
To get the total salary given out to the employees,
SELECT SUM (salary) FROM employee;
ORDER BY
ORDER BY will sort the records and display them. We can use a single column or multiple
column.
ra
Example:
SELECT * FROM SALES ORDER BY SaleDate;
SELECT * FROM SALES ORDER BY SaleDate, InvoiceNo;
ith
SELECT * FROM SALES ORDER BY SaleDate DESC, Salesperson ASC;
CONSTRAINTS
SQL constraints are used to specify rules for the data in a table. Constraints are used to limit
the type of data that can go into a table. This ensures the accuracy and reliability of the data in
the table. If there is any violation between the constraint and the data action, the action is aborted.
v
Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.
Pa
Used to create and retrieve data from the database very quickly
64 A PRACTICAL WORKBOOK FOR CBSE COMPUTER SCIENCE (PYTHON)–XII
Create a Table
ra
v ith
Pa
SQL 65
Alter Table
ra
ith
Drop Table
v
Pa
Truncate
66 A PRACTICAL WORKBOOK FOR CBSE COMPUTER SCIENCE (PYTHON)–XII
Aim
To write SQL queries to demonstrate DML commands.
INSERT
ra
ith
Update
v
Pa
SQL 67
Delete
ra
Table: GRADUATE
MIN(AVERAGE)
63
SUM(STIPEND)
80
AVG(STIPEND)
420
COUNT (DISTINCTSUBJECT)
4
68 A PRACTICAL WORKBOOK FOR CBSE COMPUTER SCIENCE (PYTHON)–XII
and c.
Table: FLIGHT
Ans:
ra
a) SELECT FLCODE, START, DESTINATION, NO_FLIGHTS FROM FLIGHT ORDER BY NO_
ith
FLIGHTS DESC
b) MAX(NO_FLIGHTS) 7
c) DELHI 3
MUMBAI 2
v
KANPUR 1
INDORE 1
Pa
Table: STOCK
Dcode Dname
101 Vikash Stationers
102 Bharat Drawing Emporium
103 Banaras Books Corporation
SQL 69
(i) to display details of all items in the stock table in descending order of Stkdate.
Ans. SELECT * FROM STOCK ORDER BY Stkdate;
(ii) to increase unitpr (unit price) by 20% in the stock table for the items which has been pur-
chased before 2010.
Ans. UPDATE STOCK SET unitpr=unitpr+(unitpr*0.2) WHERE Stkdate<’01-JAN-2010’;
(iii) to display the details of those items whose Dcode (Dealer Code) is 102 or Qty (Quantity) is
more than 50 from the table stock.
Ans. SELECT * FROM STOCK WHERE Dcode=102 OR Qty>50;
(iv) to display minimum unit price of items for each dealer individually as per Dealer Code from
the table stock.
Ans. SELECT DCODE,MIN(unitpr) FROM STOCK GROUP BY DCODE ;
ra
3
(ii) SELECT Qty*unitpr FROM STOCK WHERE Itcode=457;
Ans. Qty*unitpr
1575
ith
(iii) SELECT Itname, Dname FROM STOCK S, DEALERS D WHERE S.Dcode=D.Dcode AND
Itcode=450;
Ans. Itname Dname
Eraser Natraj Vikash Stationers
v
(iv) SELECT MAX(Stkdate) FROM STOCK;
Ans. MAX(Stkdate)
Pa
13-Sep-2010