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

RDBMs Codes - 2

Uploaded by

moneeshzoro
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)
116 views

RDBMs Codes - 2

Uploaded by

moneeshzoro
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/ 6

1.

Create table 1

CREATE TABLE Mobile_Master(

IME_No VARCHAR(10) PRIMARY KEY,

Model_Name VARCHAR(20),

Manufacturer VARCHAR(20),

Date_Of_Manufac DATE,

Warranty_In_Years NUMBER(10),

Price NUMBER(7,2),

Distributor_ID VARCHAR(10),

Spec_No VARCHAR(10),

FOREIGN KEY(Distributor_ID) REFERENCES Distributor(Distributor_ID),

FOREIGN KEY(Spec_No) REFERENCES Mobile_Specification(Spec_No)

);

2. Create table 2

CREATE TABLE Sales_Info(

salesid NUMBER(10) PRIMARY KEY,

sales_date DATE,

IME_No VARCHAR(10),

Price NUMBER(10),

Discount NUMBER(10),

Net_Amount NUMBER(10),

Customer_ID VARCHAR(10),

FOREIGN KEY(IME_No) REFERENCES Mobile_Master(IME_No),

FOREIGN KEY(Customer_ID) REFERENCES Customer_Info(Customer_ID));

3. Alter Table 1

ALTER TABLE Mobile_Master

ADD CONSTRAINT CHECK_WARRANTY CHECK(Warranty_in_Years > 2);


4. Alter Table 2

ALTER TABLE Sales_Info

RENAME TO Trade_Info;

5. Purge Table

TRUNCATE TABLE Customer_Info;

6. Drop Table

DROP TABLE Mobile_Specification CASCADE CONSTRAINTS;

7. Try It Out

CREATE TABLE department(

dept_id NUMBER(4) PRIMARY KEY,

prod_id NUMBER(4),

dept_name VARCHAR(25) UNIQUE,

dept_head VARCHAR(25) NOT NULL,

FOREIGN KEY(prod_id) REFERENCES PRODUCT(prod_id)

);

8. Insert Records

INSERT INTO CUSTOMER_INFO VALUES(‘C01’, ‘Peter’, ‘Alabama’, 9884564566, ‘[email protected]’);

INSERT INTO CUSTOMER_INFO VALUES(‘CO2’, ‘Johan’, ‘Colorado’, 7324565689, ‘[email protected]’);

9. Update Records 1

UPDATE Mobile_Specification SET Battery_Life_Hrs = 10;

10. Update Records 2

UPDATE Mobile_Master SET Warranty_in_Years=2, Price = 20000

WHERE Model_Name=’SamsungS2’;
11. Delete Records

DELETE Sales_Info WHERE Customer_ID = ‘MB10010’;

12. Display Customer Details

SELECT Customer_ID, Customer_Name, Mobile FROM Customer_Info ORDER BY Customer_ID;

13. Select Unique Operating SystemCoding exercise

SELECT UNIQUE(OS) FROM Mobile_Specification ORDER BY OS ASC;

14. Customers with gmail accountCoding exercise

SELECT Customer_ID, Customer_Name, Address, Email FROM Customer_Info

WHERE Email LIKE ‘%@gmail.com’ ORDER BY Customer_Name;

15. Display Customers whose name starts with S and ends with a

SELECT Customer_ID, Customer_Name, Address, Email FROM Customer_Info

WHERE Customer_Name LIKE ‘S%ai’ ORDER BY Customer_Name;

16. Mobile details based on warranty and cost

SELECT IME_No, Model_Name, Manufacturer, Date_Of_Manufac FROM Mobile_Master

WHERE Price>10000 AND Warranty_in_Years<5

ORDER BY Model_Name, IME_No DESC;

17. Mobiles based on manufacturer

SELECT IME_No, Model_Name, Manufacturer, Price FROM Mobile_Master

WHERE Manufacturer IN(‘Nokia’, ‘Samsung’);

18. Mobile details based on Distributor

SELECT IME_No, Model_Name, Manufacturer, Price FROM Mobile_Master

WHERE Distributor_ID NOT IN(‘SA111’, ‘MC111’) ORDER BY IME_No;


19. Mobile details based on price

SELECT IME_No, Model_Name, Manufacturer, Price FROM Mobile_Master

WHERE Price BETWEEN 10000 AND 20000 ORDER BY IME_No;

20. Distributor details based on email

SELECT Distributor_ID, Distributor_Name, Address, Mobilenumber FROM Distributor

WHERE Email IS NULL ORDER BY Distributor_Name DESC;

21. Credentials

SELECT Customer_Name,
CONCAT(CONCAT(SUBSTR(Customer_Name,1,3),LENGTH(Customer_Name)),SUBSTR(Mobile,-3,3))

AS CUSTOMER_PASSWORD FROM Customer_Info

ORDER BY Customer_Name DESC;

22. Customer purchase date

SELECT CONCAT(CONCAT(‘The customer id ‘,Customer_ID),CONCAT(‘ has purchased the mobile in


the month of ‘,TO_CHAR(Sales_Date,’Month’)))

AS PURCHASE_DATE FROM Sales_Info ORDER BY Customer_ID DESC;

23. Discount based on month

SELECT SalesId,

Sales_Date,

IME_No,

Price,

COALESCE(CAST(Discount AS CHAR(40)),’Not Applicable’) AS DISCOUNT

FROM Sales_Info

WHERE TO_CHAR(Sales_Date, ‘MON’)=’FEB’

ORDER BY IME_No;

24. Maximum and minimum price

SELECT MAX(Price) AS MAX_PRICE, MIN(Price) AS MIN_PRICE FROM Mobile_Master;


25. Price Calculation

SELECT SUM(Price) AS TOTAL_PRICE, ROUND(AVG(PRICE),2) AS AVG_PRICE FROM Mobile_Master;

26. Display model count

SELECT Manufacturer, COUNT(Model_Name) AS MOBILE_MODEL_COUNT FROM Mobile_Master

GROUP BY Manufacturer HAVING COUNT(Model_Name)=3 ORDER BY Manufacturer DESC;

27. Mobile count based on warranty

SELECT Manufacturer, COUNT(Model_Name) AS COUNT

FROM Mobile_Master GROUP BY Manufacturer

HAVING AVG(Warranty_in_Years)>3;

28. Mobile details with discount

SELECT d.Distributor_Name, m.Model_Name, s.Sales_Date, s.Discount

FROM Distributor d JOIN Mobile_Master m ON d.Distributor_ID=m.Distributor_Id

JOIN Sales_Info s ON m.IME_No=s.IME_No

ORDER BY d.Distributor_Name ASC;

29. Customer purchase details

SELECT ci.Customer_ID, ci.Customer_Name, s.Price

FROM Customer_Info ci

JOIN Sales_Info s ON ci.Customer_ID=s.Customer_ID

WHERE s.Price BETWEEN 15000 AND 25000

ORDER BY ci.Customer_ID ASC;

30. Android based mobiles

SELECT mm.IME_No, mm.Model_Name, mm.Date_of_Manufac

FROM Mobile_Master mm

JOIN Mobile_Specification ms ON mm.Spec_No=ms.Spec_no

WHERE ms.OS LIKE ‘%Android%’ AND ms.Battery_Life_Hrs>=8

ORDER BY mm.IME_No ASC;


31. Temperature with town

SELECT townname || ‘ has ‘ || wintertemp || ‘ Degree celsius’ AS TEMPERATURE

FROM Town

WHERE LOWER(townname)<>LOWER(‘Jaipur’) AND wintertemp>(

SELECT wintertemp

FROM Town

WHERE LOWER(townname)=LOWER(‘Jaipur’)

ORDER BY wintertemp ASC;

32. Course details based on fees

SELECT c.CourseID, c.CourseName, SUM(c.Fees) AS TOTALFEES

FROM Course c

JOIN REGISTRATION r ON c.CourseID=r.CourseID

GROUP BY c.CourseID, c.CourseName

HAVING SUM(c.Fees)>15000

ORDER BY c.CourseID ASC;

33. Mark with student name

SELECT s.student_name, MAX(m.value) AS MAX_MARK

FROM Student s

JOIN Mark m ON s.student_id = m.student_id

WHERE s.student_name LIKE ‘%i’

GROUP BY s.student_name

ORDER BY s.student_name DESC;

You might also like