SQL - CREATE Command (to
create Database and Table)
1)Creating Database
CREATE DATABASE MyDatabase;
Output
2)Creating Table
CREATE TABLE table_name
(column_name datatype [constraint],
column_name datatype [constraint],
column_name datatype [constraint],...);
CREATE TABLE student
( roll_no numeric(15) PRIMARY KEY NOT NULL,
name varchar(25),
age numeric(2),
dob date);
SQL | Arithmetic Operators
3) Addition (+)
SELECT bookno, name, cost, discount, cost+50 AS
"Final Cost"
FROM BOOKS;
Output
BookNo Name Cost Discount Final Cost
9871 Nancy Drew 200 5% 250
9560 Goosebump 250 10% 300
9810 Sherlock Holmes 300 15% 350
8700 The Blue Umbrella 200 6% 250
5086 Gulliver Travels 160 4% 210
1272 Hellen Keller 150 4% 200
4) Subtraction (-)
SELECT bookno, name, cost, discount, cost-discount AS
"Final Cost"
FROM BOOKS;
Output
BookNo Name Cost Discount Final Cost
9871 Nancy Drew 200 12.50 237.50
9560 Goosebump 250 30.00 270.00
9810 Sherlock Holmes 300 52.50 297.50
8700 The Blue Umbrella 200 15.00 235.00
5086 Gulliver Travels 160 8.40 201.6
1272 Hellen Keller 150 8.00 192.00
5) Multiplication (*)
SELECT bookno, name, cost, discount, cost*discount AS
"Final Cost"
FROM BOOKS;
Output
BookN Name Cost Discount Final Cost
o
9871 Nancy Drew 200 12.50 2500
9560 Goosebump 250 30.00 7500
9810 Sherlock 300 52.50 15750
Holmes
8700 The Blue 200 15.00 3000
Umbrella
5086 Gulliver 160 8.40 1344
Travels
1272 Hellen Keller 150 8.00 1200
6)Division (/)
SELECT bookno, name, cost, discount, cost/discount AS
"Final Cost"
FROM BOOKS;
Output
BookN Name Cost Discoun Final Cost
o t
9871 Nancy Drew 200 12.50 16.00
9560 Goosebump 250 30.00 8.33
9810 Sherlock Holmes 300 52.50 5.71
8700 The Blue 200 15.00 13.33
Umbrella
5086 Gulliver Travels 160 8.40 19.04
1272 Hellen Keller 150 8.00 18.75
7)Modulus (%)
SELECT bookno, name, cost, discount, cost%discount AS
"Final Cost"
FROM BOOKS;
Output
BookN Name Cost Discoun Final Cost
o t
9871 Nancy Drew 200 12.50 0
9560 Goosebump 250 30.00 10
9810 Sherlock Holmes 300 52.50 37.5
8700 The Blue 200 15.00 5
Umbrella
5086 Gulliver Travels 160 8.40 0.4
1272 Hellen Keller 150 8.00 6
SQL - ALTER TABLE statement (to
modify table definition)
8) To add a column in the table
ALTER TABLE Student ADD Gender varchar(1);
Output
9) To add multiple columns in
the table
ALTER TABLE Student ADD (Gender varchar(1),
Percentage varchar(11));
Output
10) To drop a column in the
table
ALTER TABLE Student DROP COLUMN Gender;
Output
SQL - GROUP BY and ORDER BY
11) Select Student and group them
by city
SELECT * FROM Student GROUP BY City;
Output
12) Select Student and group them
by city in descending order
SELECT * FROM Student
GROUP BY City
ORDER BY City DESC;
Output
SQL - WHERE Clause (to filter
data)
13) Fetch Student whose physics marks
are 91
SELECT * FROM Student WHERE physics=91;
Output
14) Fetch Student where city is Delhi
and Gwalior
SELECT * FROM Student WHERE City IN ('Gwalior',
'Delhi');
Output