0% found this document useful (0 votes)
15 views9 pages

Handson 8

Uploaded by

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

Handson 8

Uploaded by

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

create database javatraining;

use javatraining;
# Question no-1
# create worldcity table

CREATE TABLE WORLDCITY (


City VARCHAR(50),
Country VARCHAR(50),
Continent VARCHAR(50),
Latitude DECIMAL(9, 6),
NorthSouth CHAR(1),
Longitude DECIMAL(9, 6),
EastWest CHAR(1)
);
INSERT INTO WORLDCITY (City, Country, Continent, Latitude, NorthSouth, Longitude,
EastWest)
VALUES
('Athens', 'Greece', 'Europe', 37.59, 'N', 23.44, 'E'),
('Atlanta', 'United States', 'North America', 33.45, 'N', 84.23, 'W'),
('Dallas', 'United States', 'North America', 32.47, 'N', 96.47, 'W'),
('Nashville', 'United States', 'North America', 36.09, 'N', 86.46, 'W'),
('Victoria', 'Canada', 'North America', 48.25, 'N', 123.21, 'W'),
('Peterborough', 'Canada', 'North America', 44.18, 'N', 79.18, 'W'),
('Vancouver', 'Canada', 'North America', 49.18, 'N', 123.04, 'W'),
('Toledo', 'United States', 'North America', 41.39, 'N', 83.82, 'W'),
('Warsaw', 'Poland', 'Europe', 52.15, 'N', 21.00, 'E'),
('Lima', 'Peru', 'South America', 12.03, 'S', 77.03, 'W'),
('Rio De Janeiro', 'Brazil', 'South America', 22.43, 'S', 43.13, 'W'),
('Santiago', 'Chile', 'South America', 33.27, 'S', 70.40, 'W'),
('Bogota', 'Colombia', 'South America', 04.36, 'N', 74.05, 'W'),
('Buenos Aires', 'Argentina', 'South America', 34.36, 'S', 58.28, 'W'),
('Quito', 'Ecuador', 'South America', 00.13, 'S', 78.30, 'W'),
('Caracas', 'Venezuela', 'South America', 10.30, 'N', 66.56, 'W'),
('Madras', 'India', 'Asia', 28.36, 'N', 77.12, 'E'),
('Bombay', 'India', 'Asia', 18.58, 'N', 72.50, 'E'),
('Manchester', 'England', 'Europe', 51.30, 'N', 0.0, NULL),
('Moscow', 'Russia', 'Europe', 55.45, 'N', 37.35, 'E'),
('Paris', 'France', 'Europe', 48.52, 'N', 2.20, 'E'),
('Shenyang', 'China', 'Asia', 41.48, 'N', 123.27, 'E'),
('Cairo', 'Egypt', 'Africa', 30.03, 'N', 31.15, 'E'),
('Tripoli', 'Lybia', 'Africa', 32.54, 'N', 13.11, 'E'),
('Beijing', 'China', 'Asia', 39.56, 'N', 116.24, 'E'),
('Rome', 'Italy', 'Europe', 41.54, 'N', 12.29, 'E'),
('Tokyo', 'Japan', 'Asia', 35.42, 'N', 139.46, 'E'),
('Sydney', 'Australia', 'Australia', 33.52, 'S', 151.13, 'E'),
('Sparta', 'Greece', 'Europe', 37.05, 'N', 22.27, 'E'),
('Madrid', 'Spain', 'Europe', 40.24, 'N', 3.41, 'W');

select * from worldcity;


select count(*) from worldcity;

#1. For all the different countries contained in the WORLDCITY table, display their
names and the continent in which they are located.
# Make sure that no country name is duplicated.
select distinct country,continent from worldcity;

#2. Write an SQL query to display the list of the city and country for all the
cities that begin with letter R.
select city, country from worldcity where city like 'R%';

#3. Write an SQL query to display the list of the city and country for all the
cities that end with letter A.
select city,country from worldcity where city like '%A';

#4. Write an SQL query to display the list of the city and country for all the
cities that begin with letter M and
# have exactly six letters in them.
select city,country from worldcity where city like '%M_____';

#5. Write an SQL query to display the list of the city and country for all the
cities that contain an A as the second letter.
select city,country from worldcity where city like '_A%';

# Question no-2
# create customer table

create table customer (


custid int primary key,
fname varchar(30),
lname varchar(30),
address varchar(50),
phoneno varchar(15) not null,
city varchar(20),
country varchar(20),
datefirstpurchased date,
supplierid int,
foreign key (`supplierid`) references supplier(`supplierid`)
);

alter table customer rename column lanme to lname;


describe customer;

# insert date into customer table


INSERT INTO CUSTOMER (CUSTID, FNAME, LNAME, ADDRESS, PHONENO, CITY, COUNTRY,
DATEFIRSTPURCHASED, SUPPLIERID)
VALUES
(1001, 'Das', 'Jeyaseelan', '119, park Avenue, II street', '9841093428',
'Coimbatore', 'India', '2004-01-10', 1),
(2001, 'Gopi', 'Govindraj', '241, I floor, Kamaraj street, Madippakkam',
'9444124590', 'Chennai', 'India', '2005-03-25', 4),
(1202, 'Dilip', 'Kishore', '43, II Avenue, Anna Nagar', '9997234534', 'Bangalore',
'India', '2004-08-20', 2),
(1300, 'Aanand', 'Chowdhury', '42/1 sector 1, II Street', '9841054348',
'Bangalore', 'India', '2005-05-15', 2),
(1220, 'Chandra', 'Nagarajan', '83, lal bagh', '98410672356', 'Bangalore', 'India',
'2006-02-12', 4),
(1221, 'Abhishek', 'Kumar', '13,kishori park,', '94447623901', 'Chennai', 'India',
'2004-05-15', 1),
(1320, 'Nikhil', 'Pandit', '218, alwaanya street', '94448923091', 'Salem', 'India',
'2006-04-21', 3),
(1222, 'Meenu', 'Monica', 'C11, church road', '98410563421', 'Trichy', 'India',
'2004-08-30', 1),
(1225, 'Pavan', 'Kumar', '128/A, North Mada Street', '99934782103', 'Madurai',
'India', '2004-08-18', 4);
UPDATE CUSTOMER SET custid=1202 WHERE custid=1201;
select * from customer where custid=1202;

# create supplier table


CREATE TABLE supplier (
supplierid int primary key,
sname varchar(30),
scity varchar(30),
sphone varchar(15) not null,
email varchar(50) unique
);

#Insert data into SUPPLIER table


INSERT INTO SUPPLIER (SUPPLIERID, SNAME, SCITY, SPHONE, EMAIL)
VALUES
(1, 'Dilip', 'Chennai', '8999900000', '[email protected]'),
(2, 'Tarun', 'Madurai', '8999911111', '[email protected]'),
(3, 'Naresh', 'Coimbatore', '8999922222', '[email protected]'),
(4, 'Ganesan', 'Trichy', '8999933333', '[email protected]');

# create orders table


create table orders (
orderid int primary key,
orderdate date,
custid int,
quantity int check (quantity > 0),
itemid int,
foreign key (custid) references customer(custid)
);

# Insert data into ORDERS table


INSERT INTO ORDERS (orderid, orderdate, custid, quantity, itemid)
VALUES
(1, '2004-01-12', 1001, 30, 25),
(2, '2005-05-06', 1202, 38, 24),
(3, '2006-12-16', 1220, 10, 22),
(4, '2004-05-21', 1222, 12, 21);

insert into orders values(4, '2004-05-21', 1222, 12, 21);

-- create items table


create table items (
itemid int primary key,
itemname varchar(35) not null,
supplierid int,
minqty int not null,
maxqty int not null,
price decimal(5,2),
foreign key (supplierid) references supplier(supplierid)
);

# Insert data into ITEMS table


INSERT INTO ITEMS (ITEMID, ITEMNAME, SUPPLIERID, MINQTY, MAXQTY, Price)
VALUES
(20, 'Pears Soap', 4, 7, 20, 30.00),
(21, 'V.V.D. Coconut oil 200 ml', 2, 8, 15, 79.00),
(22, 'Ponds powder 400g', 3, 6, 25, 106.00),
(23, 'Reynolds pen- blue', 1, 10, 30, 15.00),
(24, 'Reynolds pen- black', 1, 10, 30, 16.00),
(25, 'Mysore sandal soap', 4, 7, 25, 25.00),
(26, 'Fair & lovely cream- 50g', 3, 5, 15, 55.00),
(27, 'Rexono deo spary', 2, 5, 20, 100.00),
(28, 'Dove soap', 4, 7, 15, 85.00);

#1. Display all customers from Chennai.


SELECT * FROM CUSTOMER WHERE CITY = 'Chennai';

#2. Display the details of all customers who purchased from the supplier 2.
SELECT c.* FROM CUSTOMER c JOIN ORDERS o ON c.CUSTID = o.CUSTID JOIN ITEMS i ON
o.ITEMID = i.ITEMID WHERE i.SUPPLIERID = 2;

#3. Display CUSTID, FNAME, LNAME of all customers whose purchase date is after
January 2005.
SELECT CUSTID, FNAME, LNAME FROM CUSTOMER WHERE DATEFIRSTPURCHASED > '2005-01-01';

#4. Display the details of all suppliers who are from location Coimbatore.
SELECT * FROM SUPPLIER WHERE SCITY = 'Coimbatore';

#5. Display the details of all suppliers whose name stars with G.
SELECT * FROM SUPPLIER WHERE SNAME LIKE 'G%';

#6. Display the details of all customers, who do not have the alphabet e in their
LNAME.
SELECT * FROM CUSTOMER WHERE LNAME NOT LIKE '%e%';

#7. Display the details of the entire customer whose first date of purchase is in
2006 and arrange it in descending order.
SELECT * FROM CUSTOMER WHERE DATEFIRSTPURCHASED >= '2006-01-01' ORDER BY
DATEFIRSTPURCHASED DESC;

#8. Display the details of all the orders where the quantity is less than 35.
SELECT * FROM ORDERS WHERE QUANTITY < 35;

#9. Display the details of the items supplied by supplier 4.


SELECT * FROM ITEMS WHERE SUPPLIERID = 4;

#10. Display the details of all items where SUPPLIERID is 3 and the MINQTY is
greater than 7 order by ITEMID.
SELECT * FROM ITEMS WHERE SUPPLIERID = 3 AND MINQTY > 7 ORDER BY ITEMID;
create database javatraining;
use javatraining;
# Question no-1

CREATE TABLE WORLDCITY (


City VARCHAR(50),
Country VARCHAR(50),
Continent VARCHAR(50),
Latitude DECIMAL(9, 6),
NorthSouth CHAR(1),
Longitude DECIMAL(9, 6),
EastWest CHAR(1)
);
INSERT INTO WORLDCITY (City, Country, Continent, Latitude, NorthSouth, Longitude,
EastWest)
VALUES
('Athens', 'Greece', 'Europe', 37.59, 'N', 23.44, 'E'),
('Atlanta', 'United States', 'North America', 33.45, 'N', 84.23, 'W'),
('Dallas', 'United States', 'North America', 32.47, 'N', 96.47, 'W'),
('Nashville', 'United States', 'North America', 36.09, 'N', 86.46, 'W'),
('Victoria', 'Canada', 'North America', 48.25, 'N', 123.21, 'W'),
('Peterborough', 'Canada', 'North America', 44.18, 'N', 79.18, 'W'),
('Vancouver', 'Canada', 'North America', 49.18, 'N', 123.04, 'W'),
('Toledo', 'United States', 'North America', 41.39, 'N', 83.82, 'W'),
('Warsaw', 'Poland', 'Europe', 52.15, 'N', 21.00, 'E'),
('Lima', 'Peru', 'South America', 12.03, 'S', 77.03, 'W'),
('Rio De Janeiro', 'Brazil', 'South America', 22.43, 'S', 43.13, 'W'),
('Santiago', 'Chile', 'South America', 33.27, 'S', 70.40, 'W'),
('Bogota', 'Colombia', 'South America', 04.36, 'N', 74.05, 'W'),
('Buenos Aires', 'Argentina', 'South America', 34.36, 'S', 58.28, 'W'),
('Quito', 'Ecuador', 'South America', 00.13, 'S', 78.30, 'W'),
('Caracas', 'Venezuela', 'South America', 10.30, 'N', 66.56, 'W'),
('Madras', 'India', 'Asia', 28.36, 'N', 77.12, 'E'),
('Bombay', 'India', 'Asia', 18.58, 'N', 72.50, 'E'),
('Manchester', 'England', 'Europe', 51.30, 'N', 0.0, NULL),
('Moscow', 'Russia', 'Europe', 55.45, 'N', 37.35, 'E'),
('Paris', 'France', 'Europe', 48.52, 'N', 2.20, 'E'),
('Shenyang', 'China', 'Asia', 41.48, 'N', 123.27, 'E'),
('Cairo', 'Egypt', 'Africa', 30.03, 'N', 31.15, 'E'),
('Tripoli', 'Lybia', 'Africa', 32.54, 'N', 13.11, 'E'),
('Beijing', 'China', 'Asia', 39.56, 'N', 116.24, 'E'),
('Rome', 'Italy', 'Europe', 41.54, 'N', 12.29, 'E'),
('Tokyo', 'Japan', 'Asia', 35.42, 'N', 139.46, 'E'),
('Sydney', 'Australia', 'Australia', 33.52, 'S', 151.13, 'E'),
('Sparta', 'Greece', 'Europe', 37.05, 'N', 22.27, 'E'),
('Madrid', 'Spain', 'Europe', 40.24, 'N', 3.41, 'W');
select * from worldcity;
select count(*) from worldcity;

#1. For all the different countries contained in the WORLDCITY table, display their
names and the continent in which they are located.
# Make sure that no country name is duplicated.
select distinct country,continent from worldcity;

#2. Write an SQL query to display the list of the city and country for all the
cities that begin with letter R.
select city, country from worldcity where city like 'R%';

#3. Write an SQL query to display the list of the city and country for all the
cities that end with letter A.
select city,country from worldcity where city like '%A';

#4. Write an SQL query to display the list of the city and country for all the
cities that begin with letter M and
# have exactly six letters in them.
select city,country from worldcity where city like '%M_____';

#5. Write an SQL query to display the list of the city and country for all the
cities that contain an A as the second letter.
select city,country from worldcity where city like '_A%';

# Question no-2
# create customer table
create table customer (
custid int primary key,
fname varchar(30),
lname varchar(30),
address varchar(50),
phoneno varchar(15) not null,
city varchar(20),
country varchar(20),
datefirstpurchased date,
supplierid int,
foreign key (`supplierid`) references supplier(`supplierid`)
);

alter table customer rename column lanme to lname;


describe customer;

# insert date into customer table


INSERT INTO CUSTOMER (CUSTID, FNAME, LNAME, ADDRESS, PHONENO, CITY, COUNTRY,
DATEFIRSTPURCHASED, SUPPLIERID)
VALUES
(1001, 'Das', 'Jeyaseelan', '119, park Avenue, II street', '9841093428',
'Coimbatore', 'India', '2004-01-10', 1),
(2001, 'Gopi', 'Govindraj', '241, I floor, Kamaraj street, Madippakkam',
'9444124590', 'Chennai', 'India', '2005-03-25', 4),
(1202, 'Dilip', 'Kishore', '43, II Avenue, Anna Nagar', '9997234534', 'Bangalore',
'India', '2004-08-20', 2),
(1300, 'Aanand', 'Chowdhury', '42/1 sector 1, II Street', '9841054348',
'Bangalore', 'India', '2005-05-15', 2),
(1220, 'Chandra', 'Nagarajan', '83, lal bagh', '98410672356', 'Bangalore', 'India',
'2006-02-12', 4),
(1221, 'Abhishek', 'Kumar', '13,kishori park,', '94447623901', 'Chennai', 'India',
'2004-05-15', 1),
(1320, 'Nikhil', 'Pandit', '218, alwaanya street', '94448923091', 'Salem', 'India',
'2006-04-21', 3),
(1222, 'Meenu', 'Monica', 'C11, church road', '98410563421', 'Trichy', 'India',
'2004-08-30', 1),
(1225, 'Pavan', 'Kumar', '128/A, North Mada Street', '99934782103', 'Madurai',
'India', '2004-08-18', 4);

UPDATE CUSTOMER SET custid=1202 WHERE custid=1201;


select * from customer where custid=1202;

# create supplier table


CREATE TABLE supplier (
supplierid int primary key,
sname varchar(30),
scity varchar(30),
sphone varchar(15) not null,
email varchar(50) unique
);

#Insert data into SUPPLIER table


INSERT INTO SUPPLIER (SUPPLIERID, SNAME, SCITY, SPHONE, EMAIL)
VALUES
(1, 'Dilip', 'Chennai', '8999900000', '[email protected]'),
(2, 'Tarun', 'Madurai', '8999911111', '[email protected]'),
(3, 'Naresh', 'Coimbatore', '8999922222', '[email protected]'),
(4, 'Ganesan', 'Trichy', '8999933333', '[email protected]');

# create orders table


create table orders (
orderid int primary key,
orderdate date,
custid int,
quantity int check (quantity > 0),
itemid int,
foreign key (custid) references customer(custid)
);

# Insert data into ORDERS table


INSERT INTO ORDERS (orderid, orderdate, custid, quantity, itemid)
VALUES
(1, '2004-01-12', 1001, 30, 25),
(2, '2005-05-06', 1202, 38, 24),
(3, '2006-12-16', 1220, 10, 22),
(4, '2004-05-21', 1222, 12, 21);

insert into orders values(4, '2004-05-21', 1222, 12, 21);

-- create items table


create table items (
itemid int primary key,
itemname varchar(35) not null,
supplierid int,
minqty int not null,
maxqty int not null,
price decimal(5,2),
foreign key (supplierid) references supplier(supplierid)
);
# Insert data into ITEMS table
INSERT INTO ITEMS (ITEMID, ITEMNAME, SUPPLIERID, MINQTY, MAXQTY, Price)
VALUES
(20, 'Pears Soap', 4, 7, 20, 30.00),
(21, 'V.V.D. Coconut oil 200 ml', 2, 8, 15, 79.00),
(22, 'Ponds powder 400g', 3, 6, 25, 106.00),
(23, 'Reynolds pen- blue', 1, 10, 30, 15.00),
(24, 'Reynolds pen- black', 1, 10, 30, 16.00),
(25, 'Mysore sandal soap', 4, 7, 25, 25.00),
(26, 'Fair & lovely cream- 50g', 3, 5, 15, 55.00),
(27, 'Rexono deo spary', 2, 5, 20, 100.00),
(28, 'Dove soap', 4, 7, 15, 85.00);

#1. Display all customers from Chennai.


SELECT * FROM CUSTOMER WHERE CITY = 'Chennai';

#2. Display the details of all customers who purchased from the supplier 2.
SELECT c.* FROM CUSTOMER c JOIN ORDERS o ON c.CUSTID = o.CUSTID JOIN ITEMS i ON
o.ITEMID = i.ITEMID WHERE i.SUPPLIERID = 2;

#3. Display CUSTID, FNAME, LNAME of all customers whose purchase date is after
January 2005.
SELECT CUSTID, FNAME, LNAME FROM CUSTOMER WHERE DATEFIRSTPURCHASED > '2005-01-01';

#4. Display the details of all suppliers who are from location Coimbatore.
SELECT * FROM SUPPLIER WHERE SCITY = 'Coimbatore';

#5. Display the details of all suppliers whose name stars with G.
SELECT * FROM SUPPLIER WHERE SNAME LIKE 'G%';

#6. Display the details of all customers, who do not have the alphabet e in their
LNAME.
SELECT * FROM CUSTOMER WHERE LNAME NOT LIKE '%e%';

#7. Display the details of the entire customer whose first date of purchase is in
2006 and arrange it in descending order.
SELECT * FROM CUSTOMER WHERE DATEFIRSTPURCHASED >= '2006-01-01' ORDER BY
DATEFIRSTPURCHASED DESC;

#8. Display the details of all the orders where the quantity is less than 35.
SELECT * FROM ORDERS WHERE QUANTITY < 35;

#9. Display the details of the items supplied by supplier 4.


SELECT * FROM ITEMS WHERE SUPPLIERID = 4;

#10. Display the details of all items where SUPPLIERID is 3 and the MINQTY is
greater than 7 order by ITEMID.
SELECT * FROM ITEMS WHERE SUPPLIERID = 3 AND MINQTY > 7 ORDER BY ITEMID;

You might also like