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

SQL Notes

This document provides a comprehensive overview of SQL commands, including their syntax and descriptions, such as CREATE, ALTER, DROP, and SELECT. It also covers SQL aggregate functions, constraints, and examples of DML commands, showcasing how to manipulate and query data in a database. Additionally, it includes practical exercises for applying SQL concepts in real scenarios.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Notes

This document provides a comprehensive overview of SQL commands, including their syntax and descriptions, such as CREATE, ALTER, DROP, and SELECT. It also covers SQL aggregate functions, constraints, and examples of DML commands, showcasing how to manipulate and query data in a database. Additionally, it includes practical exercises for applying SQL concepts in real scenarios.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CHAPTER

6 SQL

QUICK REVIEW

SQL Commands at a Glance


Command Syntax Description Example

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

name modify COLUMN c


DROP DROP TABLE table_ Delete the table from the DROP TABLE employee
name database
TRUNCATE TRUNCATE TABLE table_ Remove all data in a table TRUNCATE TABLE
name; employee;

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

SELECT SELECT c o l u m n 1 , Displays all data from a S E L E C T * FROM


column 2… table employee
FROM table_name WHERE salary>100000
WHERE [condition] GROUP BY desig, city
GROUP BY column1, ORDER BY name, id
column2
ORDER BY column1,
column2

SQL GROUP FUNCTIONS

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

Some constraints are:


Ensures that a column cannot have a NULL value
UNIQUE: Ensures that all values in a column are different
PRIMARY KEY:
in a table
FOREIGN KEY:
CHECK:

Used to create and retrieve data from the database very quickly
64 A PRACTICAL WORKBOOK FOR CBSE COMPUTER SCIENCE (PYTHON)–XII

LAB EX. 1: SQL DDL COMMANDS

Create a Table

Create a Table with Constraints

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

LAB EX. 2: SQL DML COMMANDS

Aim
To write SQL queries to demonstrate DML commands.

INSERT

ra
ith
Update
v
Pa
SQL 67

Delete

LAB EX. 3: GROUP FUNCTIONS


statement based on table GRADUATE:

ra
Table: GRADUATE

S. NO. NAME STIPEND SUBJECT AVERAGE DIV.


1 KARAN 400 PHYSICS 68 I
2 DIWAKAR
ith
450 COMP. Sc. 68 I
3 DIVYA 300 CHEMISTRY 62 I
4 REKHA 350 PHYSICS 63 I
5 ARJUN 500 MATHS 70 I
6 SABINA 400 CHEMISTRY 55 II
v
7 JOHN 250 PHYSICS 64 I
8 ROBERT 450 MATHS 68 I
Pa

9 RUBINA 500 COMP. Sc. 62 I


10 VIKAS 400 MATHS 57 II

MIN(AVERAGE)
63

SUM(STIPEND)
80

AVG(STIPEND)
420

COUNT (DISTINCTSUBJECT)
4
68 A PRACTICAL WORKBOOK FOR CBSE COMPUTER SCIENCE (PYTHON)–XII

LAB EX. 4: GROUP BY CLAUSE

and c.
Table: FLIGHT

START DESTINATION NO_STOPS


IC101 Delhi Agartala 1 5
IC102 Mumbai Sikkim 1 3
IC103 Delhi Jaipur 0 7
IC105 Kanpur Chennai 2 2
IC107 Mumbai Kanpur 0 4
IC431 Indore Chennai 3 2
IC121 Delhi Ahmedabad 2 6

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

LAB EX. 5: JOIN TABLES

Table: STOCK

Itcode Itname Dcode Qty Stkdate


444 Drawing Copy 101 110 21 31-July-2010
445 Sharpener Camlin 102 235 3 01-Aug-2010
450 Eraser Natraj 101 40 2 17-Aug-2010
452 Gel Pen Montex 103 50 5 30-Dec-2009
457 Geometry Box 101 35 45 15-Nov-2009
467 Parker Premium 102 60 205 27-Oct-2009
469 103 32 25 13-Sep-2010
Table: DEALERS

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 ;

(i) SELECT COUNT(DISTINCT Dcode)FROM STOCK;


Ans. COUNT(DISTINCT 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

You might also like