0% found this document useful (0 votes)
22 views8 pages

RDBMS

The document contains SQL commands for creating, altering, and manipulating tables, inserting and updating records, joining tables, aggregating data, and filtering results. Examples include creating tables, adding constraints, renaming tables, truncating tables, dropping tables, inserting records, updating records, deleting records, selecting data with filters, joins, aggregation functions, and ordering.

Uploaded by

mishravivekrs952
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)
22 views8 pages

RDBMS

The document contains SQL commands for creating, altering, and manipulating tables, inserting and updating records, joining tables, aggregating data, and filtering results. Examples include creating tables, adding constraints, renaming tables, truncating tables, dropping tables, inserting records, updating records, deleting records, selecting data with filters, joins, aggregation functions, and ordering.

Uploaded by

mishravivekrs952
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/ 8

1.

Create table 1:

CREATE TABLE Subject(


subject_id NUMBER,
subject_name VARCHAR(30),
subject_code VARCHAR(10),
staff_id NUMBER REFERENCES staff(staff_id),
PRIMARY KEY(subject_id)
);
----------------------------------------------------------------------

2. Create table 2:

CREATE TABLE Mark(


value NUMBER,
subject_id NUMBER REFERENCES Subject(subject_id),
student_id NUMBER REFERENCES Student(student_id),
PRIMARY KEY(subject_id, student_id)
);
----------------------------------------------------------------------

3. Alter table 1:

ALTER TABLE course


ADD CONSTRAINT check_fees CHECK (fees > 500);
----------------------------------------------------------------------

4. Alter table 2:

ALTER TABLE town RENAME TO Municipality;


----------------------------------------------------------------------
5. Purge table:

TRUNCATE TABLE town;


----------------------------------------------------------------------

6. Drop table:

DROP TABLE course CASCADE CONSTRAINTS;


----------------------------------------------------------------------

7. Try it Out:

CREATE TABLE department(


dept_id NUMBER(4),
prod_id NUMBER(4),
dept_name VARCHAR(25) UNIQUE,
dept_head VARCHAR(25) NOT NULL,
PRIMARY KEY(dept_id),
FOREIGN KEY(prod_id) REFERENCES PRODUCT(prod_id)
);
----------------------------------------------------------------------

8. Insert Records:

INSERT INTO payments VALUES('PT1','TT5',1005,'DT1');


INSERT INTO payments VALUES('PT2','TT6',1004,'DT2');
----------------------------------------------------------------------

9. Update Records 1:

UPDATE review SET comments = 'GOOD'


----------------------------------------------------------------------
10. Update Records 2:

UPDATE schedule SET source='Chennai', destination='Coimbatore'


WHERE schedule_id = 'S4';
----------------------------------------------------------------------

11. Delete Records:

DELETE payments WHERE payment_id='P3';


----------------------------------------------------------------------

12. Display specific student details:

SELECT student_name,city FROM student ORDER BY student_name


----------------------------------------------------------------------

13. Display Unique Cities:

SELECT distinct city FROM student ORDER BY city DESC


----------------------------------------------------------------------

14. Staff name starts with S:

SELECT staff_name FROM staff


WHERE staff_name LIKE 'S%'
ORDER BY staff_name
----------------------------------------------------------------------
15. Course Name based on a condition:

SELECT coursename FROM course


WHERE coursename LIKE 'C%' ORDER BY coursename
----------------------------------------------------------------------

16. Display based on Winter Temperature of Cities:

SELECT townname FROM town WHERE wintertemp>15


ORDER BY wintertemp DESC;
----------------------------------------------------------------------

17. Student ID based on city:

SELECT studid, firstname, lastname, street FROM student


WHERE city LIKE 'Chennai' OR city LIKE 'Bangalore' ORDER BY firstname;
----------------------------------------------------------------------

18. List of Department names:

SELECT department_name FROM department


WHERE department_block_number != 3
ORDER BY department_name;
----------------------------------------------------------------------

19. Display Town based on Summer Temperature:

SELECT townname FROM town


WHERE summertemp BETWEEN 30 AND 45
ORDER BY summertemp;
----------------------------------------------------------------------
20. Display contact details of guests:

SELECT guestid, name, email, phone FROM guest


WHERE phone IS NOT NULL
ORDER BY name;
----------------------------------------------------------------------

21. User credentials:

SELECT name,CONCAT(CONCAT(SUBSTR(name,1,2),LENGTH(name)),SUBSTR(phno,-
3,3))
AS user_password FROM users
ORDER BY name desc;
----------------------------------------------------------------------

22. Formatting Date:

SELECT CONCAT(CONCAT(schedule_id,' is scheduled on the month of ',


TO_CHAR(travel_date,'Month')) AS schedule_date FROM schedule
ORDER BY schedule_id DESC;
----------------------------------------------------------------------

23. Contact details:

SELECT guestid,name,
COALESCE(phone,COALESCE(address, COALESCE(email,'NOT AVAILABLE'))) AS
contact_info FROM guest ORDER BY guestid;
----------------------------------------------------------------------
24. Display student count:

SELECT department_id, COUNT(student_id) AS student_count FROM student


GROUP BY department_id ORDER BY student_count DESC;
----------------------------------------------------------------------

25. Display resort rating:

SELECT MAX(starrating) AS "HIGHEST RATING", MIN(starrating) AS "LOWEST


RATING"
FROM RESORT
----------------------------------------------------------------------

26. Display Average Mark:

SELECT student_id, ROUND(AVG(value),0) AS avg_mark FROM mark


GROUP BY student_id
HAVING AVG(value)<75
ORDER BY avg_mark;
----------------------------------------------------------------------

27. Display Guest Details:

SELECT resortid, (adultcount+petcount) AS total_guest


FROM booking
WHERE childcount=0
ORDER BY resortid;
----------------------------------------------------------------------
28. Display staff with department details:

SELECT student_name, staff_name, department_name FROM staff


JOIN subject ON staff.staff_id=subject.staff_id
JOIN department ON staff.department_id = department.department_id
ORDER BY staff_name;
----------------------------------------------------------------------

29. Department based student details:

SELECT student_name, department_name


FROM student JOIN department
ON student.department_id = department.department_id
WHERE UPPER(student.city) <> 'COIMBATORE' AND
department.department_name = 'MECH'
ORDER BY student.student_name;
----------------------------------------------------------------------

30. Display Student details based on Condition:

SELECT student_name, city FROM student


WHERE department_id NOT IN (SELECT department_id FROM department
WHERE department_name <> 'CSE') ORDER BY student_name;
----------------------------------------------------------------------

31. Cities based on Temperature:

SELECT townname,state FROM town WHERE wintertemp<


(SELECT wintertemp FROM town WHERE UPPER(townname) = 'PONDICHERRY')
ORDER BY townname;
----------------------------------------------------------------------
32. Mark details:

SELECT subject_name, min(value) AS min_mark


FROM subject JOIN mark
ON subject.subjecy_id = mark.subject_id
WHERE initcap(subject_name) IN ('Software Engineering','Computer
Programming')
GROUP BY subject_name
GROUP BY subject_name DESC;
----------------------------------------------------------------------

33. Display Total Amount details:

SELECT guest.guestid, name, sum(totalcharge) AS total_amount_paid


FROM guest JOIN booking
ON guest.guestid = booking.guestid
GROUP BY guest.guestid, guest.name
HAVING SUM(totalcharge)<=60000
ORDER BY guest.name DESC;
----------------------------------------------------------------------

You might also like