Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

SQL Final

The document contains examples of SQL queries to create tables, insert data, alter tables by adding columns, and drop tables. The queries demonstrate how to create tables for employee and bank details, insert data into the tables, find aggregate functions like max, min and average, add an age column to a table, display highest paid employees, and drop a table.

Uploaded by

niloorampoyam08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Final

The document contains examples of SQL queries to create tables, insert data, alter tables by adding columns, and drop tables. The queries demonstrate how to create tables for employee and bank details, insert data into the tables, find aggregate functions like max, min and average, add an age column to a table, display highest paid employees, and drop a table.

Uploaded by

niloorampoyam08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Q1.

Create a Table as employee and the details are :


S.No. Name Designation Branch
1 Ram Manager Chennai
2 Santosh Superviser Madurai
3 Hari Assistant Trichy

Ans –
CREATE TABLE employee(
S.No. int,
Name varchar(30),
Designation varchar(30),
Branch varchar(30),
);

INSERT INTO employee(S.No., Name, Designation, Branch)


VALUES (1,'Ram', 'Manager', 'Chennai');

INSERT INTO employee(S.No., Name, Designation, Branch)


VALUES (2,'Santosh', 'Superviser', 'Madurai');

INSERT INTO employee(S.No., Name, Designation, Branch)


VALUES (3,'Hari', 'Assistant', 'Trichy');
Q2. Write a Insert Query for Bank Details:
S.No. Cust.Name AccNo Balance CustBranch
1 Ramesh 12378 100000 Adyar
2 Sam 12367 152500 Mylapore
3 Harish 12345 250000 Anna Salai

Ans –
CREATE TABLE Bank_details(
S.No. int,
Cust.Name varchar(30),
AccNo int(30),
Balance int(20),
CustBranch varchar(30),
);

INSERT INTO Bank_details (S.No. , Cust.Name, AccNo, Balance , CustBranch)


VALUES (1,'Ramesh', 12378 , 100000 , 'Adyar ' );

INSERT INTO Bank_details (S.No. , Cust.Name, AccNo, Balance , CustBranch)


VALUES (2,'Sam', 12367, 152500, ' Mylapore ' );

INSERT INTO Bank_details (S.No. , Cust.Name, AccNo, Balance , CustBranch)


VALUES (3,' Harish ', 12345, 250000, ' Anna Salai ' );
Q3. Find the maximum salary, the minimum salary and the average salary
among all employees:
Ans –
Assume we have alredy created a table, named ‘emp’ and their values are –
E.id Ename Address Salary
01 Sanjay Raipur 50000
02 Rahul Delhi 30000
03 Vikas Mumbai 45000
04 Priya Kondagaon 40000
05 Neha Jaipur 35000

Query For Finding Maximum Salary


Select MAX(Salary) from emp;
Output – MAX(Salary)
50000

Query For Finding Minimum Salary


Select MIN(Salary) from emp;
Output – MIN(Salary)
30000

Query For Finding Average of Salary


Select AVG(Salary) from emp;
Output – AVG(Salary)
40000
. Q4.
Write a query to look at temperature for july(7)from table START,
lower temperature first, city name and latitude?
Ans.-
Assume we have alredy created a table, named ‘START’ and their values are –
Id City Latitude Month Temperature
01 Kondagaon 111 07 July 32
02 Raipur 112 10 July 45
03 Janjgir 113 15 July 52
04 Mainpat 114 20 July 20
05 Jashpur 115 25 July 38

SQL query:
SELECT city, latitude, temperature
*FROM START
WHERE month = 7
ORDER BY temperature ASC, city ASC, latitude ASC;

Output –
Id City Latitude Month Temperature
04 Mainpat 114 20 July 20
01 Kondagaon 111 05 July 32
05 Jashpur 115 25 July 38
02 Raipur 112 10 July 45
03 Janjgir 113 15 July 52
. Q5. Create a check constraint that MONTH should between 1 and 12 ?
Ans. –
Assume we have alredy created a table, named ‘Month’ and their values are –
mid month
01 January
02 Febuary
03 March
04 April
05 May

Query For Check Constraint -


ALTER TABLE Month ADD CHECK (month >= 1 AND month <= 12);

Note- This statement adds a check constraint to the table that ensures that the
"month" column can only contain values between 1 and 12.
Q6. Write a query in sql to create a table employee and department.
Employee(empno, ename, deptno, job, hiredate)
Department(deptno, dname, loc) ?
Ans –
CREATE TABLE Employee(
empno. int,
ename varchar(30),
deptno int(30),
job varchar(30),
hiredate varchar(30),
);

CREATE TABLE Department(


deptno. int(30),
dname varchar(30),
loc varchar(30),
);
Q7 Insert following values in employee table.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

Ans –
CREATE TABLE emp(
EMPNO int,
ENAME varchar(30),
JOB varchar(30),
MGR varchar(30),
HIREDATE varchar(30),
SAL int(20),
COMM int(20),
DEPTNO int(20),
);

INSERT INTO emp (EMPNO , ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO)
VALUES (7369,' SMITH ', ‘CLERK’ , 7902, ‘17-DEC-80’ , 800 , , 20 );

INSERT INTO emp (EMPNO , ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO)
VALUES (7499,' ALLEN ', ‘SALESMAN, 7698, ‘20-FEB-81’ , 1600, 300 , 30 );

INSERT INTO emp (EMPNO , ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO)
VALUES (7521,' WARD ', ‘SALESMAN, 7698, ‘22-FEB-81’ , 1250, 500 , 30 );

INSERT INTO emp (EMPNO , ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO)
VALUES (7566,' JONES ', ‘MANAGER, 839, ‘02-APR-81’ , 2975, , 20);

INSERT INTO emp (EMPNO , ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO)
VALUES (7654,' MARTIN ', ‘SALESMAN, 7698, ‘28-SEP-81’ , 1250, 1400 , 30);
Q8. Add a new column in your table : AGE ? in employee table.
Ans –
Assume we have alredy created a table, named ‘emp’ and their values are –
E.id Ename Address Salary
01 Sanjay Raipur 50000
02 Rahul Delhi 30000
03 Vikas Mumbai 45000
04 Priya Kondagaon 40000
05 Neha Jaipur 35000

Query For Adding Age column in table -


ALTER TABLE emp
ADD Age int(5);
Output –
E.id Ename Address Salary Age
01 Sanjay Raipur 50000
02 Rahul Delhi 30000
03 Vikas Mumbai 45000
04 Priya Kondagaon 40000
05 Neha Jaipur 35000

*Age column Added in perticuler Table


Q9. Write a PL/SQL program to display the names of five highest paid
employees in a company?
Ans –
Assume we have alredy created a table, named ‘emp’ and their values are –
E.id Ename Paid_Salary
01 Rohan 50000
02 Sohan 30000
03 Mohan 45000
04 Neha 31000
05 Vikas 20000
06 Vinay 25000

Query -
Select * from (select * from emp order by Paid_Salary desc) Where
rownum<=5);
Output –
E.id Ename Paid_Salary
01 Rohan 50000
03 Mohan 45000
04 Neha 31000
02 Sohan 30000
06 Vinay 25000
Q10. Drop table department?
Ans –
Assume we have alredy created a table, named ‘department’ and their values
are –
d.id dname FacultyName Salary
01 Maths Mohan 50000
02 Physics Rohit 30000
03 CS Sohan 45000
04 Machenics Rishbh 40000
05 Descrete Harsh 35000

Query For drop table -


DROP TABLE department;
Output – *The existing Table department is now completely removed from
Database.

You might also like