Tuesday, 10 September 2024

SQL-2



 Consider the tables Admin and Transport given below:

Table: ADMIN

Table: TRANSPORT







CREATE TABLE ADMIN(S_ID VARCHAR(5) PRIMARY KEY,S_NAME VARCHAR(10), ADDRESS VARCHAR(10), S_TYPE VARCHAR(15));






DESC ADMIN;








INSERT INTO ADMIN(S_ID,S_NAME,ADDRESS,S_TYPE)VALUES ('S001','SANDYA','ROHINI','DAY BOARDER');

INSERT INTO ADMIN(S_ID,S_NAME,ADDRESS,S_TYPE)VALUES('S002','VEDANSHI','ROTHAK','DAY SCHOLAR');

INSERT INTO ADMIN(S_ID,S_NAME,ADDRESS,S_TYPE)VALUES('S003','VIBHU','RAJ NAGAR','NULL');

INSERT INTO ADMIN(S_ID,S_NAME,ADDRESS,S_TYPE)VALUES('S004','ATHARVA','RAMPUR','DAY BOARDER');

SELECT * FROM ADMIN;







CREATE TABLE TRANSPORT(S_ID VARCHAR(5) PRIMARY KEY, BUS_NO VARCHAR(6), STOP_NAME VARCHAR(17));

DESC TRANSPORT;






INSERT INTO TRANSPORT(S_ID,BUS_NO,STOP_NAME)VALUES('S002','TSS10','SARAI KALE KHAN');

INSERT INTO TRANSPORT(S_ID,BUS_NO,STOP_NAME)VALUES('S004','TSS12','SAINIK VIHAR');

INSERT INTO TRANSPORT(S_ID,BUS_NO,STOP_NAME)VALUES('S005','TSS10','KAMLA NAGAR');

SELECT * FROM TRANSPORT;







Write SQL queries for the following:

(i)  Display the student name and their stop name from the table Admin and Transport.

Ans.   SELECT S_NAME,STOP_NAME FROM ADMIN,TRANSPORT WHERE ADMIN.S_ID=TRANSPORT.S_ID;






(ii) Display the number of students whose S_TYPE is not known.

Ans. SELECT COUNT(*) FROM ADMIN WHERE S_TYPE IS NULL;







(iii) Display all details of the students whose name starts with 'v',

Ans. SELECT * FROM ADMIN WHERE S_NAME LIKE 'V%';








(iv) Display student id and address in alphabetical order of student name, from the table Admin.

Ans. SELECT S_ID, ADDRESS FROM ADMIN ORDER BY S_NAME;







SQL-1

 create database CSIP;

us CSIP;

consider the table ORDERS given below and write the output of the SQL queries that follow


CREATE TABLE ORDERS(ORDNO INTEGER PRIMARY KEY, ITEM VARCHAR(12),QTY INTEGER, RATE INTEGER, ORDATE DATE);





DESC ORDERS;\






INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1001,'RICE',23,120,'2023-09-10');

INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1002,'PULSES',13,120,'2023-10-18');

INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1003,'RICE',25,110,'2023-11-17');

INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1004,'WHEAT',28,120,'2023-12-25');

INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1005,'PULSES',16,110,'2024-01-15');

INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1006,'WHEAT',27,55,'2024-04-15');

INSERT INTO ORDERS(ORDNO,ITEM,QTY,RATE,ORDATE) VALUES(1007,'WHEAT',25,60,'2024-04-30');


SELECT * FROM ORDERS;


(i) SELECT ITEM, SUM(QTY) FROM ORDERS GROUP BY ITEM;

Ans.


(ii) SELECT ITEM, QTY FROM ORDERS WHERE  ORDATE BETWEEN '2023-11-01' AND '2023-12-31';

Ans.






(iii) SELECT  ORDNO, ORDATE FROM ORDERS WHERE ITEM = 'WHEAT' AND RATE >=60;
Ans.