You are on page 1of 15

SQL FATAFAT – SHORT HAND

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

ENAME DNAME JOB EMPNO HIREDATE LOC

ROWS ADAMS RESEARCH CLERK 7876 23-MAY-87 DALLAS


ALLEN SALES SALESMAN 7499 20-FEB-81 CHICAGO
BLAKE SALES MANAGER 7698 01-MAY-81 CHICAGO
CLARK ACCOUNTING MANAGER 7782 09-JUN-81 NEW YORK
FORD RESEARCH ANALYST 7902 03-DEC-81 DALLAS
JAMES SALES CLERK 7900 03-DEC-81 CHICAGO
JONES RESEARCH MANAGER 7566 02-APR-81 DALLAS
KING ACCOUNTING PRESIDENT 7839 17-NOV-81 NEW YORK
MARTIN SALES SALESMAN 7654 28-SEP-81 CHICAGO
MILLER ACCOUNTING CLERK 7934 23-JAN-82 NEW YORK
SCOTT RESEARCH ANALYST 7788 19-APR-87 DALLAS
SMITH RESEARCH CLERK 7369 17-DEC-80 DALLAS
TURNER SALES SALESMAN 7844 08-SEP-81 CHICAGO
WARD SALES SALESMAN 7521 22-FEB-81 CHICAGO

Table / Relation – Employee


Columns / Attributes / Fields – ENAME, DNAME, JOB, EMPNO, HIREDATE,LOC
Rows / Records / Tuple – 14 records are here in the table
Degree – Number of Columns
Cardinality – Number of Rows
In Employee Table
Degree – 6, Cardinality – 14
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
KEY CONSTRAINTS
Primary Key - It is a column or set of column which can uniquely identify each row in a table but it
does not accept NULL values.
E.g. – RollNo, EmpCode, CustomerNo, UID etc.
Unique Key - It is a column or set of column which can uniquely identify each row in a table but it
accept NULL values.
Foreign Key – It is a column in a table which is the primary key in another table.

CUSTOMER

ORDER Customer_id

Here in the above example


Customer_id in table Order is a Foreign Key which is the primary key of table Customer.

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.

Sl.No. DDL DML


Data Definition Language Data Manipulation Language

It deals with structure of table. Like It deals with data stored in


1
Table Name, Columns rows.

It is used to create, alter, drop table It is used to insert, update, delete


2
from database. the rows from table.

E.g. CREATE TABLE, ALTER TABLE, E.g. INSERT , UPDATE, DELETE,


3
DROP TABLE etd. SELECT

Differences between CHAR and VARCHAR data type.

Sl.No. CHAR VARCHAR


1 It accept fixed length text It accept variable length text
2 Memory wastage is high. It does not waste of memory
E.g. NAME CHAR(20)
If, data stored in NAME column is NAME VARCHAR(20)
“RAM” If, data stored in NAME column is
2 It will occupy memory space of 20 “RAM”
characters even if ,we are having less Then, It will occupy space of 3
number of characters in text. characters only.

Differences between PRIMARY KEY and UNIQUE KEY

Sl.No. Primary Key Unique Key


It is a column or set of column
It is a column or set of column which
which can uniquely identify each
can uniquely identify each row in a RollNo Name Class
1 RollNo Name Class row in a table.
table. 1 it acceptRaj XI
1 it does notRaj But NULL values
But accept NULLXIvalues 2 Seema XII
2 Seema XII
Null Suhana XI
3 NULL XI
4 Kavita XI
4 Kavita XI
Null Namita XI
5 Namita XI
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
E.g.
E.g.

Here RollNo is Unique key it has


Here RollNo is Primary Key because it unique value for each row and
has unique value for each row also accepts Null value.
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
Differences between Degree and Cardinality

Sl.No. Degree Cardinality


Number of rows in a table is
Number of Columns in a table is
1 known as cardinality.
known as degree
E.g. E.g.
RollNo Name Class RollNo Name Class
1 Raj XI 1 Raj XI
2 Seema XII 2 Seema XII
2 3 NULL XI 3 Null XI
4 Kavita XI 4 Kavita XI
5 Namita XI 5 Namita XI

Degree = 3 Cardinality=5

Differences between ALTER and UPDATE

Sl.No. Alter Update


1 It is a DDL Command. It is a DML command.
It is used to add, modify, remove the It is used to modify the data
2 column stored in rows
E.g. UPDATE Student
E.g. ALTER TABLE Student
SET Class=”XII”
ADD Marks INT; WHERE Name=”Kavita”;

Differences between DELETE and DROP

Sl.No. Drop Delete


1 It is a DDL Command. It is a DML command.
It is used to remove table from It is used to remove
2
database. rows/records/data from table.
E.g. DELETE FROM Customer
E.g. DROP TABLE Student;
WHERE Name is NULL;
DATA TYPES

For Numbers – INT , INT(Value), NUMBER

For Decimal Numbers – FLOAT(M,N), DECIMAL(M,N)

For String/Text – CHAR(N), VARCHAR(N)


SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
For Date - DATE
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
SQL
DDL COMMANDS
1. CREATE TABLE
SYNTAX :
CREATE TABLE Table_Name
(

Col_Name_1 DataType Constraints,


Col_Name_2 DataType Constraints,
Col_Name_3 DataType Constraints,
.
.
Col_Name_n DataType Constraints
);

e.g. :- Create table companies data are as follows…


cnum integer Primary Key
cname character(5) Not Null
city Varchar(7)
stdcode character(5)

Ans.:- CREATE TABLE companies


( cnum Cnt Primary Key,
cname Char(5),
city Varchar(7),
stdcode Char(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)
);

e.g. 1:- Add a column stdcode char(7) in table companies.

Ans.:- ALTER TABLE companies


ADD (stdcode char(7));
e.g. 2:- Modify the column stdcode varchar(7) in table companies.
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
Ans.:- ALTER TABLE companies
MODIFY (stdcode Varchar(7));
e.g. 3:- Remove the column stdcode in table companies.
Ans.:- ALTER TABLE companies
DROP stdcode;
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
3. DROP TABLE

SYNTAX:-
DROP TABLE Table_Name ;
e.g. :- drop table ‘companies’.

Ans.:-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);

1. Adding data for all columns


e.g. :- insert the following data in table ‘companies’
1001,BPL,DELHI,9413921030

Ans.:- INSERT INTO companies


VALUES(1001,”BPL”,”DELHI”,”9413921030”);
2. Inserting Column specific data
e.g. :- Add the following data in table ‘companies’ for the column cnum and cname
1002,LG
Ans.:- INSERT INTO companies(cnum, cname)
VALUES(1002,”LG”);
UPDATE
Two clause UPDATE & SET are used for changing the values stored in any cell.
SYNTAX:::-
UPDATE Table_name
SET column_name = value_expression
[WHERE condition];

e.g. 1:- update all companies with stdcode 011.


Ans.:- UPDATE companies
SET stdcode = ‘011’;
e.g. 2 :- Change the stdcode to 022 for city Mumbai.
Ans.:- UPDATE companies
SET stdcode = ‘022’
WHERE city='Mumbai;
e.g. 3 :- Modify the stdcode to 011,cname to sony and city to delhi for cnum 1002.
Ans.:- UPDATE companies
SET cname=‘Sony’ , city = ‘Delhi’ , stdcode = ‘011’
WHERE cnum=1002;
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
e.g. 4 :- Change the stdcode to 011 where city is Delhi in table companies .
Ans.:- UPDATE companies
SET stdcode = ‘011’
WHERE city=‘Delhi’;
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
DELETE
We can delete particular row(s)from a table.

SYNTAX:::-
DELETE FROM Table_Name
WHERE predicate;

e.g. :- delete all rows from companies where cnum is 1003.

Ans.:-DELETE FROM companies


WHERE cnum=1003;
SELECT

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];

Displaying all columns using * keyword


e.g. :- Display all data from table COMPANIES.
cnum cname city stdcode
Ans :- SELECT *
FROM companies; 1001 BPL Delhi 011
1002 Akai Mumbai 022
1003 Voltas Chennai 044

Displaying Specific Column/columns 1004 IBM Delhi 011

e.g. :- Display city and stdcode from table COMPANIES.


Ans :- SELECT city, stdcode OUTPUT:
FROM companies;
city stdcode
Delhi 011
Mumbai 022
Chennai 044
Delhi 011
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
WHERE :- The WHERE keyword is used to select group of records based on some condition(s).

Conditional selection used in the WHERE clause :-


=,>,<,>=,<=,<>,LIKE, IN, BETWEEN , IS NULL, IS NOT NULL

e.g. :- Display company name which have their offices in delhi.


Ans :- SELECT cname
FROM companies
WHERE city=‘Delhi’;

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.

Ans. cnum cname city stdcode


SELECT * 1003 Voltas Chennai 044
FROM Companies
ORDER BY cname DESC; 1004 IBM Delhi 011
1001 BPL Delhi 011
1002 Akai Mumbai 022

GROUP BY :-

1. GROUP BY clause allows the user to define a subset of the values in


particular field in term of another field.
2. It also applies an aggregate function to the subset.

e.g. :- Display highest quantity sold by each company.

Ans. SELECT cnum, MAX(qty)


FROM sales
GROUP BY cnum;
SQL FATAFAT – SHORT HAND
LOVEJEET ARORA
Class 12th CS/IP
Table : OFFICE
Department SAL Name Q.1 WAQ to display total no. of staff in table
office.
Ans. SELECT count(*)

Sales 3000 Rajesh FROM office;


Marketing 2000 Bhupesh Q.2 WAQ to display total no. of employees in each
department.
Marketing 3000 Mitali Ans. SELECT Department, count(*)
Sales 1500 Shobhna FROM office
Accounting 2100 Dipika GROUP BY Department;
sales 1900 Robin Q.3 WAQ to display total salary paid to each
department.
Accounting 2200 Shridhar Ans. SELECT Department, SUM(Sal)
Sales 5000 Ram FROM office
Marketing 2100 Soniya GROUP Depatment;
HR 9000 Kailash Department SUM(Sal)
Sales 11400
Marketing 7100
Accounting 4300
HR 9000

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

You might also like