Date : Experiment No:
Program : Creating, inserting values, updating and display a table in many forms
using SQL Queries
Aim:
To creating, inserting values, updating and display a table in many
forms using SQL Queries.
A: Creating a table:
mysql> use mysql
Database changed
mysql> create table student(sid int(5), sname char(20), gender char(7), city
char(30), phonenum int(10));
Query OK, 0 rows affected (0.08 sec)
mysql> create table marks(sid int(5), Computer int, Physics int, Chemistry int,
Biology int);
Query OK, 0 rows affected (0.08 sec)
B: Inserting values into table:
mysql> insert into student values(101,'Sathya','Male','Krishnagiri',2467453);
Query OK, 1 row affected (0.03 sec)
mysql> insert into student values (102,'Sindhu,'Female','Hosur',2467834),
(103,'Karthick','Male','Krishnagiri', 2389345), (104,'Anitha','Female','Salem',
2387865), (105,'Vimal','Male','Dharmapuri', 2356895);
Query OK, 4 row affected (0.03 sec)
Records : 4 Duplicates : 0 Warnings : 0
mysql> insert into marks values (101,89,67,58,90), (102,78,98,65,45),
(103,56,78,83,97), (104,99,48,69,75), (105,87,82,79,60);
Query OK, 5 row affected (0.03 sec)
Records : 5 Duplicates : 0 Warnings : 0
C: Displaying a table:
mysql> select * from student;
sid sname gender city phonenum
101 Sathya Male Krishnagiri 2467453
102 Sindhu Female Hosur 2467834
103 Karthick Male Krishnagiri 2389345
104 Anitha Female Salem 2387865
105 Vimal Male Dharmapuri 2356895
5 rows in set (0.00 sec)
mysql> select * from marks;
sid Computer Physics Chemistry Biology
101 89 67 58 90
102 78 98 65 45
103 56 78 83 97
104 99 48 69 75
105 87 82 79 60
5 rows in set (0.00 sec)
D: Selecting Specific Rows using WHERE clause
mysql> select * from student where sid=103;
sid sname gender city phonenum
103 Karthick Male Krishnagiri 2389345
1 rows in set (0.03 sec)
E: Sorting Results - ORDER BY clause
mysql> select * from student order by sname desc;
sid sname gender city phonenum
105 Vimal Male Dharmapuri 2356895
102 Sindhu Female Hosur 2467834
101 Sathya Male Krishnagiri 2467453
103 Karthick Male Krishnagiri 2389345
104 Anitha Female Salem 2387865
5 rows in set (0.00 sec)
mysql> select * from student where gender='Male' order by sname;
sid sname gender city phonenum
103 Karthick Male Krishnagiri 2389345
101 Sathya Male Krishnagiri 2467453
105 Vimal Male Dharmapuri 2356895
3 rows in set (0.01 sec)
F: Aggregate Functions
mysql> select sum(Computer) from marks;
sum(Computer)
409
1 rows in set (0.03 sec)
mysql> select avg(Computer) from marks;
avg(Computer)
81.8000
1 rows in set (0.01 sec)
mysql> select count(*) from marks;
Count(*)
5
1 rows in set (0.02 sec)
mysql> select count(distinct city) from student;
count(distinct address)
4
1 rows in set (0.01 sec)
mysql> select min(Biology), max(Chemistry) from marks;
min(Biology) max(Chemistry)
45 83
1 rows in set (0.05 sec)
G: The Alter Table Commands
mysql> alter table marks add(Total int, Average int);
Query OK, 5 row affected (0.16 sec)
Records : 5 Duplicates : 0 Warnings : 0
mysql> select * from marks;
sid Compute Physics Chemistry Biology Total Average
r
101 89 67 58 90 NULL NULL
102 78 98 65 45 NULL NULL
103 56 78 83 97 NULL NULL
104 99 48 69 75 NULL NULL
105 87 82 79 60 NULL NULL
5 rows in set (0.00 sec)
H: The Update Commands:
mysql> update marks set Total = Computer + Physics + Chemistry + Biology,
Average=Total/4;
Query OK, 5 row affected (0.06 sec)
Row matched : 5 Changed : 5 Warnings : 0
mysql> select * from marks;
sid Computer Physics Chemistr Biology Total Average
y
101 89 67 58 90 304 76
102 78 98 65 45 286 72
103 56 78 83 97 314 79
104 99 48 69 75 291 73
105 87 82 79 60 308 77
5 rows in set (0.00 sec)
I: Join tables
mysql> select sname, Total, Average from student, marks where student.sid =
marks.sid;
sname Total Average
Karshima 304 76
Sai 286 72
Nithin
Aswin 314 79
Anitha 291 73
Bharath 308 77
5 rows in set (0.00 sec)
J: Creating Table from Existing Table
mysql> create table smarks as (select sname, Total, Average from student,
marks where student.sid = marks.sid);
Query OK, 5 row affected (0.05 sec)
Records : 5 Duplicates : 0 Warnings : 0
mysql> select * from smarks;
sname Total Average
Karshima 304 76
Sai 286 72
Nithin
Aswin 314 79
Anitha 291 73
Bharath 308 77
5 rows in set (0.00 sec)
K: The Delete Command
mysql> Delete from smarks where Total<300;
Query OK, 2 row affected (0.06 sec)
mysql> select * from smarks;
sname Total Average
Karshima 304 76
Aswin 314 79
Bharath 308 77
3 rows in set (0.00 sec)
L: The Drop table Command
mysql> Drop table smarks;
Query OK, 0 row affected (0.03 sec)
mysql> select * from smarks;
ERROR 1146(42S02): Table 'mysql.smarks' doesn't exist
M: Grouping Result - Group By
mysql> select * from emplayee;
Customer_I
Customer_Name Country
D
111 Latha Australia
112 Saranya India
113 Sathya Brazil
114 Karthick India
115 Shnajay Australia
116 Triya Australia
117 Sugachini Brazil
118 Shambavi China
119 Shanjay India
120 Triya India
mysql> select Country,count(*) from emplayee group by Country;
Country count(*)
Australia 3
India 4
Brazil 2
China 1
Table Structure (Students)
CREATE TABLE Students (
StudentID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Grade VARCHAR(10)
);
Insert Data into Table
-- Inserting sample data into the Students table
INSERT INTO Students (FirstName, LastName, Age, Grade)
VALUES
('John', 'Doe', 15, '10th'),
('Jane', 'Smith', 16, '11th'),
('Robert', 'Brown', 14, '9th');
Queries
1. Select all records from the table
SELECT * FROM Students;
2. Select specific columns (FirstName and Grade)
SELECT FirstName, Grade FROM Students;
3. Select students who are 15 years old or older
SELECT * FROM Students WHERE Age >= 15;
4. Update a student's grade
UPDATE Students
SET Grade = '12th'
WHERE StudentID = 2; -- Updating Jane Smith's grade
5. Delete a student record
DELETE FROM Students
WHERE StudentID = 3; -- Deleting Robert Brown's record
6. Count the total number of students
SELECT COUNT(*) FROM Students;
7. Select students who belong to the 10th grade
SELECT * FROM Students
WHERE Grade = '10th';
8. Find the student with the highest age
SELECT * FROM Students
WHERE Age = (SELECT MAX(Age) FROM Students);
Table Structure (Employees)
sql
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
Phone VARCHAR(15),
HireDate DATE,
Salary DECIMAL(10, 2)
);
Insert Data into Table
sql
-- Inserting sample data into the Employees table
INSERT INTO Employees (FirstName, LastName, Email, Phone, HireDate, Salary)
VALUES
('John', 'Doe', '
[email protected]', '1234567890', '2023-05-10', 50000.00),
('Jane', 'Smith', '
[email protected]', '9876543210', '2022-08-15',
60000.00),
('Robert', 'Brown', '
[email protected]', '5556667777', '2021-06-20',
55000.00),
('Emily', 'Davis', '
[email protected]', '4445556666', '2020-01-25',
70000.00),
('Michael', 'Taylor', '
[email protected]', '3334445555', '2023-07-
30', 65000.00);
Queries
1. Select all records from the table
sql
SELECT * FROM Employees;
2. Select specific columns (FirstName and Salary)
sql
SELECT FirstName, Salary FROM Employees;
3. Select employees with a salary greater than 60000
sql
SELECT * FROM Employees WHERE Salary > 60000;
4. Select employees hired after a specific date
sql
SELECT * FROM Employees WHERE HireDate > '2022-01-01';
5. Count the total number of employees
sql
SELECT COUNT(*) FROM Employees;
6. Update an employee's salary
sql
UPDATE Employees
SET Salary = 75000.00
WHERE EmployeeID = 4; -- Updating Emily Davis' salary
7. Delete an employee record
sql
DELETE FROM Employees
WHERE EmployeeID = 2; -- Deleting Jane Smith's record
8. Find the average salary
sql
SELECT AVG(Salary) FROM Employees;
9. Find the employee with the highest salary
sql
SELECT * FROM Employees
WHERE Salary = (SELECT MAX(Salary) FROM Employees);
10. Select employees whose last names start with 'D'
sql
SELECT * FROM Employees
WHERE LastName LIKE 'D%';
11. Select employees hired in the year 2023
sql
SELECT * FROM Employees
WHERE YEAR(HireDate) = 2023;
12. Order employees by salary in descending order
sql
SELECT * FROM Employees
ORDER BY Salary DESC;
13. Limit the result to the first 3 records
sql
SELECT * FROM Employees
LIMIT 3;
14. Select employees with salary between 55000 and 70000
sql
SELECT * FROM Employees
WHERE Salary BETWEEN 55000 AND 70000;
15. Select employees with a specific phone number
sql
SELECT * FROM Employees
WHERE Phone = '1234567890';
16. Group employees by salary range
sql
SELECT
CASE
WHEN Salary < 60000 THEN 'Low Salary'
WHEN Salary BETWEEN 60000 AND 70000 THEN 'Mid Salary'
ELSE 'High Salary'
END AS SalaryRange,
COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY SalaryRange;
17. Get employees who are not assigned a phone number
sql
SELECT * FROM Employees
WHERE Phone IS NULL;
18. Insert a new employee with all columns specified
sql
INSERT INTO Employees (FirstName, LastName, Email, Phone, HireDate,
Salary)
VALUES ('Alice', 'Williams', '
[email protected]', '6667778888',
'2024-01-05', 72000.00);
19. Select employees with 'email' domain as 'example.com'
sql
SELECT * FROM Employees
WHERE Email LIKE '%@example.com';
20. Get employees who have been with the company for more than 2 years
sql
SELECT * FROM Employees
WHERE DATEDIFF(CURRENT_DATE, HireDate) > 730; -- 730 days = 2 years
,,Consider the following table and Write SQL queries for (I) to (IV)
TABLE : HOSPITAL
No Name Age Department Datofadm Charges Sex
1 Sandeep 65 Surgery 23/02/98 300 M
2 Ravina 24 Orthopedic 20/01/98 200 F
3 Karan 45 Orthopedic 19/02/98 200 M
4 Tarun 12 Surgery 01/01/98 300 M
5 Zubin 36 ENT 12/02/98 250 M
6 Ketaki 16 ENT 24/02/98 300 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shaliya 31 Nuclear Medicine 19/02/98 400 M
(1) To show all information about the patients of cardiology department.
(2 ) To list the names of female patients who are in orthopedic dept.
(3) To list names of all patients with their date of admission in ascending order.
To count the number of patients with age >20.
(1) To show all information about the patients of the cardiology department:
SELECT * FROM HOSPITAL WHERE Department = 'Cardiology';
(2) To list the names of female patients who are in the orthopedic department:
SELECT Name FROM HOSPITAL WHERE Department = 'Orthopedic' AND Sex = 'F';36
(3) To list names of all patients with their date of admission in ascending order:
SELECT Name, Datofadm FROM HOSPITAL ORDER BY Datofadm ASC;
(4) To count the number of patients with age > 20:
SELECT COUNT(*) AS TotalPatients FROM HOSPITAL WHERE Age > 20;