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

SQL 2

Uploaded by

beauty within us
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL 2

Uploaded by

beauty within us
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Information Management System

Lab

Bennett University
CREATE DATABASE - creates a new database
CREATE
•CREATE DATABASE databasename;

List of
CREATE TABLE - creates a new table
CREATE
•CREATE TABLE table_name ( column1 datatype,....);

queries
ALTER TABLE - modifies a table
ALTER
•ALTER TABLE table_name ADD column_name datatype;

DROP TABLE - deletes a table


DROP
•DROP TABLE table_name;
•SQL Constraints
•- CREATE TABLE table_name ( column1 datatype constraint,....);(not
null, unique,check,primary key)
SELECT SELECT - extracts data from a database

SELECT SELECT * FROM Table name;

SELECT SELECT DISTINCT column1, column2, ...FROM table_name;

UPDATE - updates data in a database


UPDATE
•UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE - deletes data from a database


DELETE
•DELETE FROM table_name WHERE condition; (AND, OR and NOT
Operators)

INSERT INTO - inserts new data into a database


INSERT
•INSERT INTO table_name (column1, column2, column3, ...) VALUES
(value1, value2, value3, ...);
MySQL NOT NULL: By default, a column can
hold NULL values. The NOT NULL constraint
Constraints enforces a column to NOT accept NULL values.
• Eg. Select position, pname, role from team where shirt_no
is NOT NULL.

UNIQUE: Ensures that all the values in columns


are unique. (Eg. Roll no, Date of Birth, tshirt no
of same team players etc.)

PRIMARY KEY: Combination of NOT NULL and


UNIQUE.

FOREIGN KEY: Uniquely identify a row/record


in any other database table.

CHECK: The check constraints ensures that all


values in a column specify certain condition.
CHECK and UNIQUE
Constraint
CREATE TABLE newbook (
book_id varchar(15) NOT NULL UNIQUE,
book_name varchar(50) ,
isbn_no varchar(15) NOT NULL UNIQUE ,
no_page int(5)
CHECK(no_page>3) ,
book_price decimal(5,2) ,
PRIMARY KEY (book_id)
);

INSERT INTO newbook (book_id, book_name, isbn_no, no_page,


book_price) values ('B1','ABC','1.0.2','20','100.00');
INSERT INTO newbook (book_id, book_name, isbn_no, no_page,
book_price) values ('B2','DEF','2.0.6',‘1','200.00');
select * from newbook;
• create table orders(
• oid int(10),
• orderdate date,
• amount varchar(20),
• cid int(10));

Orders table
• INSERT INTO orders (oid, orderdate,amount,cid)
values ('1','2020-04-04','100','1');
• INSERT INTO orders (oid, orderdate,amount,cid)
values ('2','2020-05-05','200','2');
• INSERT INTO orders (oid, orderdate,amount,cid)
values ('3','2020-06-06','300','1');
• INSERT INTO orders (oid, orderdate,amount,cid)
values ('4','2020-07-07','400','3');
• INSERT INTO orders (oid, orderdate,amount,cid)
values ('5','2020-08-08','500','4');

• select * from orders;


Select Particular records from table
• select oid, orderdate, cid from orders where
amount >=300;
oid orderdate amount cid

output
Select Distinct records from table
• select DISTINCT cid from orders;

oid orderdate amount cid

output
Delete Particular records from table
• delete from orders where amount <=200;
• select * from orders;
oid orderdate amount cid

output
Update Particular records in table
• update orders set amount=800 where oid=1;
• select * from orders;
oid orderdate amount cid

output
condition
• select oid, orderdate, amount, cid from orders
where oid>2 AND amount<500;
oid orderdate amount cid

output
• AND
• OR
• NOT equal to ( <> )
SORTING
• select *from orders ORDER BY amount;

oid orderdate amount cid

output

• ASCENDING- ORDER BY column_name;


• DESCENDING – ORDER BY column__name DESC;
LIMIT
• select *from orders ORDER BY amount LIMIT
2;
oid orderdate amount cid

output
LIMIT 10 would return the first 10 rows
matching the SELECT criteria.
OFFSET
• select *from orders ORDER BY amount LIMIT 5
OFFSET 3;
oid orderdate amount cid

output

OFFSET is used to specify which row should be


fetched first.
Create table customers(
cid int(10),
cname varchar(30),

Customers cemail varchar(20));

Table INSERT INTO customers


(cid,cname,cemail) values
('1','A','[email protected]');
INSERT INTO customers
(cid,cname,cemail) values
('2','B','[email protected]');
INSERT INTO customers
(cid,cname,cemail) values
('3','C','[email protected]');
INSERT INTO customers
(cid,cname,cemail) values
('5','D','[email protected]');
select * from customers;
Orders table
create table orders(
oid int(10),
orderdate date,
amount varchar(20),
cid int(10));

INSERT INTO orders (oid, orderdate,amount,cid) values ('1','2020-04-04','100','1');


INSERT INTO orders (oid, orderdate,amount,cid) values ('2','2020-05-05','200','2');
INSERT INTO orders (oid, orderdate,amount,cid) values ('3','2020-06-06','300','1');
INSERT INTO orders (oid, orderdate,amount,cid) values ('4','2020-07-07','400','3');
INSERT INTO orders (oid, orderdate,amount,cid) values ('5','2020-08-08','500','4');

select * from orders;


Orders table
create table orders(
oid int(10),
orderdate date,
amount varchar(20),
cid int(10));

INSERT INTO orders (oid, orderdate,amount,cid) values ('1','2020-04-04','100','1');


INSERT INTO orders (oid, orderdate,amount,cid) values ('2','2020-05-05','200','2');
INSERT INTO orders (oid, orderdate,amount,cid) values ('3','2020-06-06','300','1');
INSERT INTO orders (oid, orderdate,amount,cid) values ('4','2020-07-07','400','3');
INSERT INTO orders (oid, orderdate,amount,cid) values ('5','2020-08-08','500','4');

select * from orders;


Between Clause
select * from orders where amount >=200 and
amount <=500;
oid orderdate amount cid

select * from orders where amount BETWEEN 200


and 500;
Group By Clause
• It groups a set of rows by values of columns or
expressions.
• It returns one row for each group.
• It is used with aggregate functions such as
SUM, AVG, MAX, MIN and COUNT.
Group By Clause Contd…
Syntax:
Select c1, c2 …..
From Tablename
where condition
Group By c1, c2, …, cn;
Example:
SELECT cid, count(*)
FROM orders
GROUP BY cid;
HAVING Clause
• It is added to SQL because the WHERE keyword could not be used with
aggregate functions.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
Count, Group By, IN, MAX, MIN
Clause
Note: In operator allows you to specify
oid orderdate amount cid
multiple values in a where clause

SELECT COUNT(*) FROM orders;


cid count
SELECT cid, COUNT(*) FROM orders GROUP BY cid;
oid amount cid
SELECT oid, amount, cid FROM orders where amount
IN(100,300,500);

SELECT MAX(amount) FROM orders;

SELECT MIN(amount) FROM orders;


SUM, AVG, RAND, SQRT Clause
oid orderdate amount cid

SELECT SUM(amount) FROM orders;

SELECT AVG(amount) FROM orders;

SELECT RAND( ), RAND( ), RAND( );

select SQRT(16);

Note: NULL values are ignored.


SQL Aliases
• SQL aliases are used to give a table, or a column in a table, a temporary
name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of the query.
Syntax
SELECT column_name AS alias_name
FROM table_name;
Example:
SELECT CONCAT(City, " ", Country) AS Address FROM Customers;
Advantages:
• There are more than one table involved in a query
• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined together

Note: It requires double quotation marks or square brackets if the alias


name contains spaces:
LIKE clause
• The LIKE operator is used in a WHERE clause to search
for a specified pattern in a column.
• There are two wildcards used in conjunction with the LIKE
operator:
▪ % - The percent sign represents zero, one, or multiple
characters
▪ _ - The underscore represents a single character
• LIKE Syntax •
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
LIKE clause contd…
create table orders(
cid int(10),
cust_name varchar (10),
oid int(10),
amount int(20));

INSERT INTO orders (cid, cust_name, oid, amount) values (1, 'abc', 1231, 500);
INSERT INTO orders (cid, cust_name, oid, amount) values (1, 'abc', 1232, 500);
INSERT INTO orders (cid, cust_name, oid, amount) values (2, 'def', 1233, 500);
INSERT INTO orders (cid, cust_name, oid, amount) values (3, 'ghi', 1234, 500);

select * from orders;

select * from orders where cust_name LIKE ‘b%';


create table A(
one varchar(5),
UNION
two varchar(5));

create table B(
three varchar(5),
four varchar(5));

INSERT INTO A (one, two) values ('a','b');


INSERT INTO A (one, two) values ('a','c');
INSERT INTO A (one, two) values ('a','d');
INSERT INTO B (three, four) values ('b','c');
INSERT INTO B (three, four) values ('a','d');

SELECT * from A; SELECT * from B;


Duplicate
SELECT one, two FROM A UNION ALL SELECT three, four FROM B; Rows

SELECT one, two FROM A UNION SELECT three, four FROM B;

No Duplicate
Rows
Orders table
create table orders(
oid int(10),
orderdate date,
amount varchar(20),
cid int(10));

INSERT INTO orders (oid, orderdate,amount,cid) values ('1','2020-04-04','100','1');


INSERT INTO orders (oid, orderdate,amount,cid) values ('2','2020-05-05','200','2');
INSERT INTO orders (oid, orderdate,amount,cid) values ('3','2020-06-06','300','1');
INSERT INTO orders (oid, orderdate,amount,cid) values ('4','2020-07-07','400','3');
INSERT INTO orders (oid, orderdate,amount,cid) values ('5','2020-08-08','500','4');

select * from orders;


Customers Table
Create table customers(
cid int(10),
cname varchar(30),
cemail varchar(20));

INSERT INTO customers (cid,cname,cemail) values ('1','A','[email protected]');


INSERT INTO customers (cid,cname,cemail) values ('2','B','[email protected]');
INSERT INTO customers (cid,cname,cemail) values ('3','C','[email protected]');
INSERT INTO customers (cid,cname,cemail) values ('5','D','[email protected]');
select * from customers;
Thank you

You might also like