SQL
QUERIES
23 | P a g e
Q1. Write down syntax and mysql program to create a database STUDENT and its
OUTPUT.
SYNTAX:
CREATE DATABASE dbname;
PROGRAM:
mysql> create database student;
Q2. Write down syntax and mysql program to delete a database STUDENT
and its OUTPUT.
SYNTAX:
DROP DATABASE dbname;
PROGRAM:
mysql> drop database student;
24 | P a g e
Q3. Write down a mysql program to create a table TEACHER with the following field names
given below and its OUTPUT.
a. Teacher_ID,
b. First_Name,
c. Last_Name,
d. Gender,
e. Salary,
f. Date_of_Birth
g. Dept_No
PROGRAM:
mysql> CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);
25 | P a g e
Q4. Write down mysql program to show the databases and tables created?
PROGRAM:
mysql> show databases;
PROGRAM
mysql> show tables;
26 | P a g e
Q5. Write a mysql program to list out the field name Teacher_ID, First_Name, Last_Name,
Gender, Salary, Date_of_Birth, Dept_No along with its field type and constraints for the table
TEACHER in the database STUDENT.
PROGRAM
mysql > use student;
Database changed
mysql > desc teacher;
27 | P a g e
Q6. Write down mysql program to set a default value for the field SALARY as 30000 to
the table name TEACHER whose field names are listed below
a. Teacher_ID,
b. First_Name,
c. Last_Name,
d. Gender,
e. Salary,
f. Date_of_Birth
g. Dept_No
PROGRAM
mysql > desc teacher;
mysql > ALTER TABLE TEACHER ALTER SALARY SET DEFAULT 30000;
28 | P a g e
Q7. Write a mysql command to INSERT VALUES INTO table TEACHER in its field
Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No
PROGRAM:
mysql > INSERT INTO Teacher (Teacher_ID, First_Name, Last_Name, Gender, Salary,
Date_of_Birth, Dept_No) VALUES(101,"Shanaya", "Batra", 'F', 50000, '1984-08-11', 1);
29 | P a g e
Q8. Write a mysql command to display the details of a Teacher whose
Teacher_ID=101.
The field names in Teacher table is listed below.
Teacher_ID,
First_Name,
Last_Name,
Gender,
Salary,
Date_of_Birth,
Dept_No
PROGRAM
mysql > SELECT * FROM TEACHER WHERE Teacher_ID=101;
30 | P a g e
Q9. Write a mysql command to UPDATE the details of Salary as 55000 in Teacher
table whose Teacher_ID=101.
The field names in Teacher table is listed below.
Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No
PROGRAM
mysql> UPDATE Teacher SET Salary=55000 WHERE Teacher_ID=101;
31 | P a g e
Q10. Write a mysql command to find the following using AGGREGATE FUNCTIONS in Table
Teacher. The field names in Teacher table is listed below.
Teacher_ID, First_Name, Last_Name, Gender, Salary, Date_of_Birth, Dept_No
(i) Calculate the sum of salary given to the Teachers and name it as Total_Salary.
(ii) Calculate the maximum and minimum salary of the Teacher and name it
as Max_Salary and Min_Salary.
(iii) Calculate the total number of Teachers whose salary is >40000.
PROGRAM 1
mysql > SELECT SUM(Salary) AS Total_Salary FROM Teacher;
32 | P a g e
PROGRAM 2
mysql > SELECT MAX(Salary) AS Max_Salary, MIN(Salary) AS Min_Salary FROM Teacher;
PROGRAM 3
mysql > SELECT COUNT(Salary) FROM Teacher WHERE Salary > 40000;
Q11. Write a mysql command to add a primary key.
PROGRAM:
mysql > ALTER TABLE item ADD PRIMARY KEY(ino);
33 | P a g e
Q12. Write a mysql command to drop a primary key.
PROGRAM:
mysql > ALTER TABLE item DROP PRIMARY KEY;
Q13. Write a mysql command to display item record which name begins from s.
PROGRAM:
Select * from item where itemname like ‘s%’;
34 | P a g e
Q14. Write a mysql command to add a unique key.
PROGRAM:
Q15. Write a mysql command to display name in upper case.
PROGRAM:
mysql> select upper(pname) as 'patient name, year(admitdate) as 'admit year' -> from hospital;
35 | P a g e
Made and Design by : Mr. Shiv Kailash Shukla
36 | P a g e