0% found this document useful (0 votes)
24 views4 pages

Xii CS Slow Leaners - SQL

Future purpose

Uploaded by

yazhinivel2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Xii CS Slow Leaners - SQL

Future purpose

Uploaded by

yazhinivel2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Double click on mysql iocon on desktop .

Provide the required password and you will be in mysql


editor
* create database here ( a memory platform )
* use database ( Activate a database to work upon )
* show databases; ( To see all the databases created so far )
* show table; ( To see that req. table is already created or not else create a new one)
2). Create a table named Emp1 with the following : ( Name is e m p one )
* mysql> create table emp1(ID_No integer not null unique, Name varchar(15) not null, Salary
decimal(10,2) not null, Doj date, Department char(10) not null);

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 ]

4). Display all records - (Select statement)


MySQL> Select * from emp1 ;

5). Display Name and date of joining of all employees : (particular columns)
MySQL> Select name, doj from emp1 ;

6). Display the records of Sales department :- (Where Clause)


MySQL> Select * from emp1 where department = 'Sales' ;

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;

(includes both the given numbers in the range )

12). Display the records of Accounts and Works department : ( IN clause )

MySQL> Select * from Emp1 where department ='Accounts' or department = 'Works';

MySQL> Select * from Emp1 where department IN ('Accounts' , 'Works' ) ;

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)

MySQL> Select Name, Salary, (10/100)*salary from Emp1 ;


((10/100)*salary is a Scalar Function)
will display the headings of the columns as

Name Sal (10/100)*salary

18). Using column Aliases : ( giving name to a column )


MySQL> Select Name, Salary, round(((10/100)*salary),2) Bonus from Emp1 ;
(Bonus is a column Aliases, rounds off to 2 decimal places)
Name Salary Bonus

MySQL>Select Name, Salary, round(((10/100)*salary),2) "Fest. Bonus" from Emp1 ;


Name Sal Fest. Bonus
(Two words column Aliases to be enclosed within " ").

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)

MySQL> Select * from emp1 where salary >25000 ;

MySQL> Select * from emp1 HAVING salary > 25000 ;

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 ;

29). Display the minimum salary among all the employees


MySQL > Select MIN(salary) from emp1;

30). Display the maximum salary among all the employees


MySQL > Select MAX(salary) from emp1;

31). Display average salary of all the employees department wise :


MySQL > Select department, round((AVG(Salary)),2) 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 ]

33). Increase the salary of all the employees by 10 %;


MySQL > UPDATE emp1 set salary = salary + (10/100)*salary;

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) ;

36) Provide all Sales departments people a City as Jabalpur .


MySQL > UPDATE EMP1 SET CITY ='JABALPUR' WHERE DEPARTMENT = 'SALES' ;

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 ;

40). Deleting a record a of an employee Mr. Atharav as he is retiring .


MySQL > DELETE from emp1 where name = ‘Atharav’ ;

You might also like