Xii CS Slow Leaners - SQL
Xii CS Slow Leaners - SQL
mysql> desc emp1; ( Description of each field you created is given here )
+---------------+------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+-----------+-------+
| ID_No | int(11) | NO | PRI | NULL | |
| Name | varchar(15) | NO | | NULL | |
| Salary | decimal(10,2) | NO | | NULL | |
| Doj | date | YES | | NULL | |
| Department | char(10) | NO | | NULL | |
+---------------+-----------------+-------+-----+-----------+-------+
3). Inserting records into the table emp1 using the following syntax : ( Example : 1st record )
mysql> insert into emp1 values(1, 'Prashant', 11000, '2021-01-10', 'Accounts');
[ Likewise we insert 10 records with 10 different insert command and values ]
mysql> select * from emp1;
+---------+------------+------------+--------------+--------------+
| ID_No | Name | Salary | Doj | Department |
+---------+------------+------------+--------------+--------------+
| 1 | Prashant | 11000.00 | 2021-01-10 | Accounts |
| 2 | Dushyant | 25000.00 | 2019-05-11 | Accounts |
| 3 | Raghav | 31000.00 | 2022-06-21 | Sales |
| 4 | Parth | 27000.00 | 2023-03-15 | Works |
| 5 | Atharav | 1900.00 | 2021-04-10 | Sales |
| 6 | Bhairav | 33900.00 | 2022-01-01 | Works |
| 7 | Rishabh | 2200.00 | 2021-01-11 | Accounts |
| 8 | Tonto | 10000.00 | 2023-01-10 | Sales |
| 9 | Rancho | 10000.00 | 2023-01-10 | Works |
| 10 | Prakhar | 5000.00 | 2023-02-12 | Accounts |
+-------+----------+----------+------------+------------+--------+
[ Note : Now, you will try all the commands and see the out put ]
5). Display Name and date of joining of all employees : (particular columns)
MySQL> Select name, doj from emp1 ;
7). Display the records of those employees who joined in the year '2021' ; ( date function)
MySQL> Select * from emp1 where year(doj) = 2021 ;
8). Display the records of those employees who have joined the company on Tuesday .
MySQL > Select * from emp1 where dayname(doj) = 'Tuesday';
9). Display the names and salary of those who have joined after the year 2021 .
MySQL > Select * from emp1 where year(doj) > 2021 ;
10). Display the Name, Salary of those worker whose salary is greater than 30000.
MySQL > Select Name, Salary from emp1 where Salary > 30000;
11). Display name and salary of persons whose salary is between 10000 to 20000.(Between clause)
MySQL> Select name, salary from emp1 where salary between 10000 and 27000;
13). Display the records except Accounts and Works department : ( NOT IN clause )
MySQL> Select * from Emp1 where department NOT IN ('Accounts' , 'Works' ) ;
14). Display all the records other than whose salary is 27000, 2200, 5000. ( NOT IN clause)
MySQL> Select * from emp1 where salary NOT IN (27000, 2200, 5000) ;
15) Display all the records in the ascending order of their names .
MySQL> select * from emp1 order by Name ;
16) Display all the records in the descending order of their names .
MySQL> Select * from emp1 order by Name desc ;
17). Display Name, Salary and 10% of Salary of all employees : (Using Scalar function)
19). Display the total salary to be paid to the workers department wise : (Use of Group By clause)
MySQL> Select department, sum(salary) from emp1 GROUP BY department ;
will display Depart Sum(salary)
Accounts 35000
Sales 75000
Works 34000
20). Display the names of all the departments . (DISTINCT Clause )
MySQL> Select DISTINCT department from emp1 ;
21). Display the records of those workers whose name starts with an alphabet 'P' . (LIKE Clause)
MySQL> Select * from emp1 where name LIKE 'P%' ;
22). Display the records of those workers whose name ends with an alphabet 'o' . (LIKE Clause)
MySQL> Select * from emp1 where name LIKE '%o' ;
23). Display the records of those workers whose name consists set of alphabets 'ra' in between then .
MySQL> Select * from emp1 where name LIKE '%ra%' ;
24). Display the records whose salary is above 25000. (HAVING Clause)
Aggregate Functions
[ Aggregate function in SQL perform a calculation on multiple values and returns a single value ]
25). Display the total number of workers in Accounts department : (Aggregate function - Count( ) )
MySQL > Select COUNT(*) from emp1 where department = 'Accounts' ;
26). Display how many number of departments are there : (Count Clause)
MySQL> Select COUNT ( DISTINCT department) from emp1 ;
27). Display how many workers are there in each department , department wise :
MySQL > Select department , COUNT(*) from emp1 GROUP BY department ;
28). Display sum of salary of all the employees in each department , department wise :
MySQL > Select department, SUM(Salary) from emp1 GROUP BY department ;
32). Display the records of those whose salary is above avg salary of the workers :-
Use of a Sub Query - A query within a query is known as a sub query , as follows :
MySQL>Select * from emp1 where salary > (select avg(salary) from emp1);
First it will take the answer to the query within brackets and then the outer query will be equated.
[ Modifying the records of the table Employee ]
34). Give 5% extra raise of Salary to the Workers of the Sales department ;
MySQL > UPDATE emp1 set salary = salary + (5/100)*salary where department = 'Sales' ;
35). Add a new column named City to the table. (ALTER with ADD Clause )
MySQL> ALTER table emp1 ADD city char(12) ;
37). Change the width of column city to 20 characters from 12. (ALTER with MODIFY Clause)
MySQL> ALTER table emp1 MODIFY city char(20);
38). Removing an already existing column from the table .(ALTER with DROP Clause )
MySQL> ALTER table emp1 DROP column city ; (word column optional here)
39). Change the name of column Doj to Date_of_joining : (ALTER with CHANGE Clause)
MySQL > ALTER table emp1 CHANGE Doj Date_of_joining Date ;