DBMS Lab
DBMS Lab
Complete solution:
/*complete code */
CREATE DATABASE college;
USE college;
CREATE TABLE dept (
deptno INT(2),
dname VARCHAR(14),
loc VARCHAR(13),
CONSTRAINT pk_dept PRIMARY KEY (deptno)
);
/* question-1 */
SELECT *
FROM emp
ORDER BY sal DESC;
/* question-2 */
SELECT *
FROM emp
WHERE sal > 8000;
/* question-3 */
SELECT *
FROM emp
WHERE sal BETWEEN 5000 AND 8000;
DBMS Lab
Prepared by Mohibul Khan kaziranga university
/* question-4 */
SELECT *
FROM emp
WHERE ename IN ('ADAMS', 'JAMES');
/* question-5 */
SELECT job, SUM(sal) AS total_salary
FROM emp
GROUP BY job
HAVING SUM(sal) > 2000;
/* question-6 */
SELECT hiredate, MAX(sal) AS max_salary
FROM emp
GROUP BY hiredate
ORDER BY max_salary ASC;
Q-1: Write a nquery to find all emploee details from emploee table order by salary decending ?
Ans:
/* question-1 */
SELECT *
FROM emp
ORDER BY sal DESC;
Q-2 : WAQ to find emploee details from employee table whose salary greater than 8000 ?
Ans:
/* question-2 */
SELECT *
FROM emp
DBMS Lab
Prepared by Mohibul Khan kaziranga university
WHERE sal > 8000;
Q-3: WAQ to find emploee details whose salary is between 5000 and 8000 ?
Ans:
/* question-3 */
SELECT *
FROM emp
WHERE sal BETWEEN 5000 AND 8000;
Q-4: WAQ to display emploee details from emploee table whose name is “ADAMS” and “JAMES” .
Ans:
/* question-4 */
SELECT *
FROM emp
WHERE ename IN ('ADAMS', 'JAMES');
Q-5: SELECT job table total salary with respect to job rom emploee table where total dsalary
greater than 2000 ?
Ans:
/* question-5 */
SELECT job, SUM(sal) AS total_salary
FROM emp
GROUP BY job
HAVING SUM(sal) > 2000;
DBMS Lab
Prepared by Mohibul Khan kaziranga university
Q-6: WAQ to display hiredate wise maximum salary order by salary ascending ?
Ans:
03/09/24
make a employee table in MySQL that will contains E,name, E.id, E.Desg, E.email.
1. Select all the employees whose name starts with "E" ?
2. Select all the employee whose name ends with “D” ?
3. Select all the employee whose name has “S” in the ?
4. Select all the employee name and their designation whose name has exactly six character ?
5. Select all the employee departmentwise ?
-- Select employee names and their designations whose names have exactly six characters
SELECT E_name, E_Desg FROM Employee
WHERE CHAR_LENGTH(E_name) = 6;
H.W:
10/09/24
DDL : Data definitioon language
• Create table
• Altering table
• Drop table
• Truncate
• Renaming
TCL :
DBMS Lab
Prepared by Mohibul Khan kaziranga university
Question :
For the table “emp” that contains “id,name,email” column
For the table “std” that contains “sid,name” column
• By Adding a new column in a table .
ALTER TABLE emp
ADD eadd VARCHAR(100)
• By chaning the size of a column
ALTER table emp
MODIFY email to email(20);
• BY changing the column name
ALTER TABLE emp CHANGE NAME emploee(30)
• By adding a foreign key
Alter table name emp
Add foreign key sid(emp)
References sid(std)
Code:
-- Step 1: Create a database
CREATE DATABASE company_db;
-- Step 6: Modify the size of the 'email' column in the 'emp' table
ALTER TABLE emp
MODIFY email VARCHAR(20);
-- Step 7: Change the column name 'name' to 'emploee' in the 'emp' table
DBMS Lab
Prepared by Mohibul Khan kaziranga university
ALTER TABLE emp
CHANGE name emploee VARCHAR(30);
-- Step 8: Add a foreign key from the 'emp' table to the 'std' table
ALTER TABLE emp
ADD CONSTRAINT fk_sid
FOREIGN KEY (sid)
REFERENCES std(sid);
Question – 2:
Create a table B.tech 3rd sem
• Id- foreighn key
• Name
• Selection
• Courses
Query:
1. Add a new column teacher
2. Make teacher as a foreign key
3. Change the column name selection to branch
4. Rename the table to BtechKU
-- Step 1: Create a database
USE company_db;
name VARCHAR(50)
);
name VARCHAR(50),
email VARCHAR(50),
);
-- Step 6: Modify the size of the 'email' column in the 'emp' table
-- Step 7: Change the column name 'name' to 'emploee' in the 'emp' table
-- Step 8: Add a foreign key from the 'emp' table to the 'std' table
REFERENCES std(sid);
id INT,
name VARCHAR(50),
selection VARCHAR(50),
courses VARCHAR(100),
);
-- Assuming there is a 'teachers' table already created, where 'id' is the primary key
REFERENCES teachers(id);
RENAME TO BtechKU;
24/09/24
DBMS Lab
Prepared by Mohibul Khan kaziranga university
DML : data manupilating language
-- question 1
SELECT sname, sadd
FROM Student
WHERE smark > 30;
-- question 2
SELECT *
FROM Student
DBMS Lab
Prepared by Mohibul Khan kaziranga university
WHERE sname LIKE '%s';
-- question 3
SELECT *
FROM Student
WHERE smark BETWEEN 70 AND 90;
-- question 4
SELECT *
FROM Student
WHERE sadd IN ('Jorhat', 'Sivsagar', 'Guwahati');
-- question 5
SELECT SUM(smark) AS total_marks, AVG(smark) AS average_marks
FROM Student;
-- question 6
SELECT COUNT(*) AS total_students
FROM Student;
1.
2.
3.
DBMS Lab
Prepared by Mohibul Khan kaziranga university
4.
5.
6.
Question:
Shoppe
ID Sname Area
S01 ABC Comp CP
S02 All Soft GK ii
S03 Tech CP
Shopee
S04 HI Tech NP
Store
Accessories
No Name Price Id
DBMS Lab
Prepared by Mohibul Khan kaziranga university
A01 Mother Bord 12000 S01
A02 Hard Disk 5000 S01
A03 Keyboard 500 S02
A04 Mouse 300 S04
A05 LCD 6000 S03
A06 Hard Disk 4500 S04
Query:
1. Too display name and price of all accessories in assending order of price ?
2. To display id and Sname of all shoppe location in NP ?
3. To display min and max price of each name of accessories ?
4. To display Name, Price of all accessories and their respective Sname where they are available ?
Answer:
-- Query 1: Display name and price of all accessories in ascending order of price
SELECT Name, Price
FROM Accessories
ORDER BY Price ASC;
-- Query 4: Display Name, Price of all accessories and their respective shop name (Sname) where they are available
SELECT A.Name, A.Price, S.Sname
FROM Accessories A
JOIN Shoppe S ON A.Id = S.ID;
1:
2:
3:
4:
DBMS Lab
Prepared by Mohibul Khan kaziranga university