Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 152

MySQL Practical File Programs (Class 12 IT 802)

In this lesson: All Class 12 SQL practical file programs on one Employee table — query, output and explanation in Hindi & English. Download-ready.

About these SQL practicals

This page brings all the Class 12 Information Technology (802) SQL practical file programs together, and builds every one of them on a single, simple Employee table. Because the same table is used throughout, you can see clearly how each command — SELECT, WHERE, ORDER BY, GROUP BY, aggregate functions, UPDATE, DELETE — changes the result.

Start by running the first two practicals once (create the table, then insert the sample rows). After that, try any query below. Each one shows the exact SQL and its output, with a short easy explanation in Hindi and English.

💡 Order matters

Run practical 1 (CREATE TABLE) and practical 2 (INSERT) first. The other queries need that table and data to exist.

1. Create the Employee table

Har SQL practical se pehle ek table chahiye. Ye CREATE TABLE se Employee table banata hai jisme id, name, department, salary aur city columns hain.

MySQL
CREATE TABLE Employee (
    emp_id     INT PRIMARY KEY,
    name       VARCHAR(40),
    department VARCHAR(30),
    salary     DECIMAL(10,2),
    city       VARCHAR(30)
);
Output:
Query OK, table 'Employee' created.

2. Insert records into Employee

Table me data INSERT INTO se aata hai. Yahan hum 5 employees ke records ek saath daal rahe hain.

MySQL
INSERT INTO Employee VALUES
(1, 'Aarav',  'IT',      55000, 'Delhi'),
(2, 'Diya',   'HR',      42000, 'Mumbai'),
(3, 'Kabir',  'IT',      61000, 'Delhi'),
(4, 'Meera',  'Sales',   38000, 'Pune'),
(5, 'Rohan',  'HR',      47000, 'Mumbai');
Output:
Query OK, 5 rows affected.

3. Display all records

SELECT * table ke saare columns aur saari rows dikhata hai. Ye sabse basic query hai.

MySQL
SELECT * FROM Employee;
Output:
+--------+-------+------------+---------+--------+
| emp_id | name  | department | salary  | city   |
+--------+-------+------------+---------+--------+
|      1 | Aarav | IT         | 55000.00| Delhi  |
|      2 | Diya  | HR         | 42000.00| Mumbai |
|      3 | Kabir | IT         | 61000.00| Delhi  |
|      4 | Meera | Sales      | 38000.00| Pune   |
|      5 | Rohan | HR         | 47000.00| Mumbai |
+--------+-------+------------+---------+--------+

4. Show selected columns

Sab columns ki jagah sirf zaroori columns dekhne ke liye unke naam likhte hain — yahan sirf name aur salary.

MySQL
SELECT name, salary FROM Employee;
Output:
+-------+---------+
| name  | salary  |
+-------+---------+
| Aarav | 55000.00|
| Diya  | 42000.00|
| Kabir | 61000.00|
| Meera | 38000.00|
| Rohan | 47000.00|
+-------+---------+

5. Filter rows with WHERE

WHERE ek shart lagata hai — sirf wahi rows aati hain jo shart poori karti hain. Yahan sirf IT department wale.

MySQL
SELECT name, department FROM Employee
WHERE department = 'IT';
Output:
+-------+------------+
| name  | department |
+-------+------------+
| Aarav | IT         |
| Kabir | IT         |
+-------+------------+

6. Employees earning above 45000

WHERE ke saath tulna operator (>) laga kar hum salary ke aadhar par rows chhaant sakte hain.

MySQL
SELECT name, salary FROM Employee
WHERE salary > 45000;
Output:
+-------+---------+
| name  | salary  |
+-------+---------+
| Aarav | 55000.00|
| Kabir | 61000.00|
| Rohan | 47000.00|
+-------+---------+

7. Sort by salary (ORDER BY)

ORDER BY result ko kisi column ke hisaab se lagata hai. DESC se bade-se-chhota.

MySQL
SELECT name, salary FROM Employee
ORDER BY salary DESC;
Output:
+-------+---------+
| name  | salary  |
+-------+---------+
| Kabir | 61000.00|
| Aarav | 55000.00|
| Rohan | 47000.00|
| Diya  | 42000.00|
| Meera | 38000.00|
+-------+---------+

8. Count employees per department

GROUP BY rows ko group me baantta hai aur COUNT() har group ki ginti karta hai.

MySQL
SELECT department, COUNT(*) AS total
FROM Employee
GROUP BY department;
Output:
+------------+-------+
| department | total |
+------------+-------+
| IT         |     2 |
| HR         |     2 |
| Sales      |     1 |
+------------+-------+

9. Total and average salary

Aggregate functions SUM() aur AVG() poore column ka jod aur औsat nikaalte hain.

MySQL
SELECT SUM(salary) AS total_salary,
       AVG(salary) AS average_salary
FROM Employee;
Output:
+--------------+----------------+
| total_salary | average_salary |
+--------------+----------------+
|    243000.00 |    48600.000000|
+--------------+----------------+

10. Highest and lowest salary

MAX() sabse badi value aur MIN() sabse chhoti value deta hai.

MySQL
SELECT MAX(salary) AS highest,
       MIN(salary) AS lowest
FROM Employee;
Output:
+----------+----------+
| highest  | lowest   |
+----------+----------+
| 61000.00 | 38000.00 |
+----------+----------+

11. Update a salary

UPDATE maujooda row ki value badalta hai. WHERE zaroori hai warna saari rows badal jayengi.

MySQL
UPDATE Employee
SET salary = 50000
WHERE name = 'Diya';
Output:
Query OK, 1 row affected.

12. Delete a record

DELETE row hata deta hai. WHERE ke bina saari rows delete ho jayengi — dhyan se.

MySQL
DELETE FROM Employee
WHERE name = 'Meera';
Output:
Query OK, 1 row affected.

13. Search with LIKE

LIKE pattern se khojta hai. 'D%' ka matlab 'D' se shuru hone wale naam.

MySQL
SELECT name FROM Employee
WHERE name LIKE 'D%';
Output:
+------+
| name |
+------+
| Diya |
+------+

14. Employees from selected cities (IN)

IN ek se zyada values me se match karta hai — yahan Delhi ya Mumbai.

MySQL
SELECT name, city FROM Employee
WHERE city IN ('Delhi', 'Mumbai');
Output:
+-------+--------+
| name  | city   |
+-------+--------+
| Aarav | Delhi  |
| Diya  | Mumbai |
| Kabir | Delhi  |
| Rohan | Mumbai |
+-------+--------+

15. Department-wise average salary

GROUP BY aur AVG() milकर har department ki औsat salary nikaalte hain.

MySQL
SELECT department, AVG(salary) AS avg_salary
FROM Employee
GROUP BY department;
Output:
+------------+-------------+
| department | avg_salary  |
+------------+-------------+
| IT         | 58000.000000|
| HR         | 44500.000000|
| Sales      | 38000.000000|
+------------+-------------+

Summary

  • All 15 SQL practicals use one Employee table, so concepts build on each other.
  • They cover the full Class 12 syllabus: create, insert, select, where, order by, group by, aggregates, update, delete, like and in.
  • Always use WHERE with UPDATE and DELETE to avoid changing every row.
  • Aggregate functions (COUNT, SUM, AVG, MAX, MIN) turn many rows into one value.

Go deeper with constraints in MySQL, normalization, or the full outer join lesson.

Frequently Asked Questions

Which table do all these SQL practicals use?
They all use one simple Employee table with columns emp_id, name, department, salary and city. Building every query on the same table makes it easy to follow how each command changes the result.
Do I run the CREATE and INSERT first?
Yes. Run practical 1 (CREATE TABLE) once, then practical 2 (INSERT) once to load the sample rows. After that you can run any SELECT, WHERE, GROUP BY or other query as many times as you like.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY. For simple row conditions like salary > 45000 you use WHERE; to filter grouped results (like departments with average salary above a value) you use HAVING.
Why must I use WHERE with UPDATE and DELETE?
Without WHERE, UPDATE changes every row and DELETE removes every row in the table. The WHERE clause limits the command to only the rows you intend, which prevents accidental data loss.
What is the difference between COUNT, SUM and AVG?
COUNT() counts how many rows, SUM() adds up a numeric column, and AVG() gives the average of a numeric column. They are called aggregate functions because they turn many rows into a single value.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.