2. Write SQL query to drop primary key from above table.
-Alter table student drop primary key;
3. Write SQL query to drop foreign key from above table.
-Alter table student drop foreign key class_id;
4. Write SQL query to set student id as primary key.
-Alter table student add primary key(Student_id);
5. Write SQL query to set class id as foreign key.
-Alter table Student
Add FOREIGN KEY (class_id) REFERENCES class(class_id)
6. Write SQL query to remove unique constraint from name.
-Alter table Student Drop constraint name;
7. Write SQL query to remove default constraint from age.
-Alter table student Alter age drop default;
8. Write SQL query to add unique constraint to section.
-Alter table student ADD UNIQUE(Section);
9. Write SQL query to add default value 18 to age.
-Alter table student Alter column age set default ‘18’;
10. Write SQL query to change column name address to location.
- Alter table Student change address location varchar(200);
11. Write SQL query to add new column email and make it not null.
-Alter table student ADD COLUMN email varchar(255) NOT NULL;
12. Write SQL query to remove column section from above table.
-Alter table student drop column section;
13. Write SQL query to add new column contact and make data type as integer.
-Alter table Student add column contact int;
14. Write SQL query to change data type of column contact to varchar and make it unique.
-Alter table Student Modify Column Contact Varchar(255) unique;
15. Write SQL query to change default value of address to Kathmandu.
-Alter table student alter column address set default ‘Kathmandu’;
16. Insert five set of records in above table.
-Insert into student(student_id,name,address,class_id,section,Age) values
(101,’Anish’,’Janakpur’,3,’C’,20);
17. Write SQL query to update name and address of student whose student id is 5.
-update student
set name=’Anish’
set address=’Janakpur’
where id=5;
18. Write SQL query to delete all the records of student having age greater than 20.
-Delete from student where age>20.
19. Write SQL query to update age of student having address btm.
-Update student
Set age=20
Where address=’btm’
20. Write SQL query to delete all records of student having student id 1.
-Delete from student where Student_id=1;
21. Write SQL query to select all records of student.
-Select * from student;
22. Write SQL query to select all records of student having student id 3.
-select * from student where Student_id=3;
23. Write SQL query to select name and address of students whose age is greater than 21.
-select name, address from student where age>21;
24. Write SQL query to select student id and name of students whose address in Birtamode.
-select student_id, name from student where address=’Birtamode’;
25. Write SQL query to select records of students whose class id is 5 and address is Kathmandu.
-Select * from student where id=5 and address=’Kathmandu’;
26. Write SQL query to select maximum age from above table.
-Select max(age) from Student;
27. Write SQL query to select minimum age of students whose address is Birtamode.
-Select min(age) from student where address=’Birtamode’;
28. Write SQL query to find total number of students having class id 5 and age greater than 19.
-Select count(*) from student where class_id=5 and age>19.
29. Write SQL query to find average age of students whose class id is 4 and section is B.
-Select avg(age) from student where class_id=4 and section=’B’;
30. Write SQL query to select students whose address starts with letter ‘B’.
-Select * from student where address like ‘B%’;
31. Write SQL query to count those students whose name ends with letter ‘R’.
- Select count(name) from student where name like ‘%R’;
32. Write SQL query to select name and age of students whose having address btm or ktm.
-Select name, age from Student where address=’btm’ or address=‘Ktm’;
33. Write SQL query to select sum of age of students having id 1,2 and 3.
-Select sum(age) from Student where id=1 or id=2 or id=3;
34. Write SQL query to select students whose age is between 18 and 22.
-Select * from Student Where Age between 18 and 22.
35. Write SQL query to select total students of each age group.
-
36. Write SQL query to select class id, name and maximum age of students studying in each class.
-
37. Write SQL query to select student’s records by arranging in descending order on the basis of student id.
-Select * from Student order by Student_id Desc;
38. Write SQL query to select student id and name by of students whose age is greater than 20 after arranging
records in alphabetical order on the basis of name.
-Select student_id, name from Student where age>20 order by name ASC;
39. Write SQL query to select records of student whose age is maximum among all the students.
-Select * from student where age=(select max(age)from student);
40. Write SQL query to select student id and name of student whose student id is maximum among all the
students.
-Select student_id, name from student where student_id=(Select max(student_id) from Student);
41. Write SQL query to select name and age of student whose age is minimum than the average age of all
students.
-Select name, age from student where age<(select avg(age) from student;
42. Write SQL query to list all the students except ‘btm & ‘ktm in asc order of age.
-Select * from Student Where address<>‘btm’ or address<>’Ktm’ order by age asc;
43. Write SQL query the students who does not belong to address ‘btm’.
-Select * from Student where address <> ‘btm,;
44. Write SQL query to display the location of ‘Ram’.
-Select address from student where name=’Ram’;
45. Write SQL query to display the total information of student table along with name and location of all the
students having address ‘Birtamode’.
-Select * from Student where address=’Birtamode’;
46. Create table below with appropriate data type and constraints. Employee Emp_Id Name Address Salary
Dept_Id Department Dept_Id Dept_Name Floor
-Create table employee (
Emp_id int primary key not null,
Name Varchar(200),
Address varchar(200),
Salary int,
Dept_Id int );
Create table Department (
Dept_Id int primary key,
Dept_name varchar(200),
Floor Varchar(200) );
47. Use all types of joins to select employee id, name and department name of employees.
-Select x.emp_id, [Link], y.Dept_name from employee x join Department y on
x.Dept_ID=y.Dept_ID;
Select x.emp_id, [Link], y.Dept_name from employee x right join Department y on
x.Dept_ID=y.Dept_ID;
Select x.emp_id, [Link], y.Dept_name from employee x left join Department y on
x.Dept_ID=y.Dept_ID;
48. Select name and address of employees whose salary is between 10000 and 20000.
-Select name, address from employees where salary between 10000 and 20000;
49. Select employee id, employee name and department name of employees working in first floor.
-Select eid,ename,depname from employees where floor=’frist’;
50. Select all records of department which are in second floor.
-Select * from department where floor=’second’;
51. Select name, address and department name of employees which are from Birtamode.
-Select name,address,depname from employees where address=’Birtamode’;
52. Select employee id and name of employees having salary more than 10000 and from Kathmandu.
-Select employee id,name from employee where salary >10000 and address=’Kathmandu’;
53. Select name, department name and floor of employee whose name start with letter ‘R’ and age is greater
than 30.
-Select name, depname, floor from employee where name like(‘R%’) and age >30;
54. Select employee id and department name of employees whose floor is ‘first’ by arranging in ascending
order on the basis of salary.
-select employee id, depname from employees where floor=’first’ order by salary asc;
55. Select total number of employee working in each department.
-Select count(emp_id) from department;
56. Select maximum salary of employee working in each floor and whose department is ‘Finance’.
-Select max(salary) from
57. Select name and department name of employees whose salary is greater than average salary of all
employees.
-Select name, depname from employees where salary> (select avg(Salary)from employee);
58. Select name and address of employee where salary is between 20000 and 30000 and floor is ‘second’.
-Select name, address from employee where salary between 20000 and 30000 and floor=’Second’;
59. Select name and department name employee whose age is minimum.
-Select name,depname from employee where age=(select min(age) from employee);
60. Select sum of salary of all employees whose name ends with letter ‘s’ and department is ‘Account’
-Select sum(salary) from employee where name like(%s) and depname=’Account’;