Professional Documents
Culture Documents
LOVEJEET ARORA
Class 12th CS/IP
Unit: 2 Database Query Using SQL
Database: A database is a collection of interrelated data.
DBMS (Database Management System) :A DBMS refers to a software that is responsible for
storing, maintaining and utilizing databases. A database along with DBMS is referred to as a
database system.
Database System : A database system is basically a computer based record keeping system.
Table : Employee
COLUMNS
CUSTOMER
ORDER Customer_id
Candidate Key – There may be two or more attributes which can become the primary key of table.
These all are candidate keys.
Alternate Key – Out of all Candidate keys one will be selected as Primary key and remaining will be
known as Alternate keys.
OTHER CONSTRAINTS
NOT NULL – Not Null constraint does not accept Null values.
E.g. CREATE TABLE Customer
(SID integer NOT NULL,
Last_Name varchar(30) NOT NULL,
First_Name varchar(30));
CHECK - CHECK constraint ensures that all values in a column satisfy certain conditions.
e.g. – CREATE TABLE Customer
(SID integer CHECK(SID>0),
Last_Name varchar(30),
First_Name varchar(30));
DEFAULT – Default constraint accept default value if no value assigned in the column.
e.g. – CREATE TABLE Customer
( SID integer,
Last_Name varchar(30),
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
First_Name varchar(30) DEFAULT “Not Assigned”,
Price Float(5,2) DEFAULT 0
);
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
Differences between DDL and DML.
Degree = 3 Cardinality=5
2. ALTER TABLE
Whenever the definition of existing table needs to be changed the ALTER command is
used to modify the table. ALTER Command works on Columns.
SYNTAX:-
ALTER TABLE Table_name
ADD/MODIFY/DROP(column1_name datatype(size),
column2_name datatype(size),
…………………………………
columni_name datatype(size)
);
SYNTAX:-
DROP TABLE Table_Name ;
e.g. :- drop table ‘companies’.
DML COMMANDS
INSERT
This command is used to add/insert data in the table.
SYNTAX:::-
INSERT INTO Table_Name
VALUES(value1,value2, ........... ,valuei);
SYNTAX:::-
DELETE FROM Table_Name
WHERE predicate;
Select command is used to retrieve/ fetch/ extract or display data from the Table.
SYNTAX :
SELECT [DISTINCT|ALL] column_name1,column_name2,….
FROM table_name1
[WHERE Condition]
[ORDER BY Ordering_column [ASC|DESC]
[GROUP BY Grouping_coumn_name]
[HAVING Predicate];
cname
BPL
IBM
ORDER BY
ORDER BY :-
1. ORDER BY clause is used to produce output in a logical order.
2. The order may be of Ascending or Descending for numbers or alphabet.
3. The keyword ASC and DESC are used for Ascending or Descending order.
Note : By default order by arranges all records in Ascending order.
e.g. :- Display all details from table companies in descending order of their company name.
GROUP BY :-
Q.4 WAQ to find department wise maximum and minimum salary paid .
Ans. SELECT Department, MAX(Sal), MIN(Sal)
FROM office
GROUP BY Department;
HAVING CLAUSE – used to apply condition on group function (min, max, sum,avg ,count)
Q.5 WAQ to display only those departments where no. of employees/ records are more than
two.
Ans. SELECT Department, Count(*)
FROM office
GROUP BY Department
WHERE count(*)>2; ERROR
HAVING count(*)>2; Correct one
Q.6 WAQ to display names of those departments where maximum salary paid is equal or more
than 5000.
Ans.
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
WRONG ANSWER
SELECT Department, Max(Sal)FROM office
GROUP BY Department; Department Max(Sal)Sales 5000
Marketing 3000
Accounting 2200
HR 9000