0% found this document useful (0 votes)
50 views

Record Mysql Emp

1. The document describes creating a database and tables called 12B, emp, and dept using MySQL. It inserts sample data into the tables. 2. Various SQL queries are performed to select, update, group, and order data from the tables. Operations include filtering on conditions, aggregating data, and modifying data. 3. The document shows how to create a database schema and manipulate data using common SQL statements.

Uploaded by

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

Record Mysql Emp

1. The document describes creating a database and tables called 12B, emp, and dept using MySQL. It inserts sample data into the tables. 2. Various SQL queries are performed to select, update, group, and order data from the tables. Operations include filtering on conditions, aggregating data, and modifying data. 3. The document shows how to create a database schema and manipulate data using common SQL statements.

Uploaded by

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

MY SQL-1

1. Create a Database 12B;

mysql> create database 12B;


Query OK, 1 row affected (0.00 sec)

mysql> use 12B;


Database changed

2. Create table emp with theFollowing attributes:


+----------+----------
Field | Type |
+----------+----------
empno | int(11) |
| ename | char(20) |
| job | char(20) |
| mgr | int(11) |
| hiredate | date |
| sal | float |
| comm | float |
| deptno | int(11) |
+----------+----------+
mysql> CREATE TABLE emp (empno int, ename char(20), job char(20), mgr int,
hiredate date, sal float(20), comm float(20), deptno int);
Query OK, 0 rows affected (0.21 sec)

3. Insert 5 rows into the table


mysql> insert into emp values ( 7839, "KING","PRESIDENT",NULL,"2010-11-01",5000
NULL,10);
Query OK, 1 row affected (0.05 sec)
mysql> insert into emp values ( 7698, "BLAKE","MANAGER",7839,"2010-12-01",2850,
ULL,30);
Query OK, 1 row affected (0.06 sec)
mysql> insert into emp values ( 7782, "CLARKE","MANAGER",7839,"2010-01-01",2450
NULL,10);
Query OK, 1 row affected (0.03 sec)
mysql> insert into emp values ( 7684, "MARTIN","SALESMAN",7698"2012-09-01",125
,400,30);
Query OK, 1 row affected (0.05 sec)
mysql> insert into emp values ( 7900, "JAMES","CLERK",7782,"2012-10-28",950,NULL
,10);
Query OK, 1 row affected (0.03 sec)

1
4. Create table dept with the following attributes:.
----------+----------+---
| Field | Type |
+----------+----------+--
| DEPTNO | int(11) |
| DNAME | char(20) |
| LOCATION | char(20)
| +----------+----------+----

mysql> CREATE TABLE DEPT ( DEPTNO INT, DNAME CHAR(20), LOCATION


CHAR(20));
Query OK, 0 rows affected (0.25 sec)

5. Insert 3 rows into the table DEPT


mysql> INSERT INTO DEPT VALUES (10, 'ACCOUNTS', 'BANGALORE');
Query OK, 1 row affected (0.11 sec)
mysql> INSERT INTO DEPT VALUES (20, 'HR', 'CHENNAI');
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO DEPT VALUES (30, 'PROCESS', 'NEWYORK');
Query OK, 1 row affected (0.01 sec)

6. Display the table dept


7. SELECT * FROM DEPT;
+--------+----------+-----------+
| DEPTNO | DNAME | LOCATION |
+--------+----------+-----------+
| 10 | ACCOUNTS | BANGALORE |
| 20 | HR | CHENNAI |
| 30 | PROCESS | NEWYORK |
+--------+----------+-----------+
3 rows in set (0.00 sec)

8. Display the table emp

mysql> SELECT * FROM EMP;


+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
| 7839 | KING | PRESIDENT | NULL | 2010-11-01 | 5000 | NULL | 10 |
| 7698 | BLAKE | MANAGER | 7839 | 2010-12-01 | 2850 | NULL | 30 |
| 7782 | CLARKE | MANAGER | 7839 | 2010-01-01 | 2450 | NULL | 10 |
| 7684 | MARTIN | SALESMAN | 7698 | 2012-09-01 | 1250 | 400 | 30 |
| 7900 | JAMES | CLERK | 7782 | 2012-10-28 | 950 | NULL | 10 |
+-------+--------+-----------+------+------------+------+------+--------+
5 rows in set (0.00 sec)

2
9. Display the different deptno

mysql> SELECT DISTINCT DEPTNO FROM EMP;


+--------+
| DEPTNO |
+--------+
| 10 |
| 30 |
+--------+
2 rows in set (0.06 sec)

10. Display the names of employees in department number 10

mysql> SELECT ENAME FROM EMP WHERE DEPTNO=10;


+--------+
| ENAME |
+--------+
| KING |
| CLARKE |
| JAMES |
+--------+
3 rows in set (0.05 sec)

11. Display the names and salary of employees who earn a salary between 2500 and
5000( both inclusive)

mysql> SELECT ENAME, SAL FROM EMP WHERE SAL BETWEEN 2500 AND 5000;
+-------+------+
| ENAME | SAL |
+-------+------+
| KING | 5000 |
| BLAKE | 2850 |
+-------+------+
2 rows in set (0.00 sec)

12. Display the employee number and name of all managers

mysql> SELECT EMPNO,ENAME FROM EMP WHERE JOB= 'MANAGER';


+-------+--------+
| EMPNO | ENAME |
+-------+--------+
| 7698 | BLAKE |
| 7782 | CLARKE |
+-------+--------+
2 rows in set (0.03 sec)

3
13. . Display the names of employees who are not managers

mysql> SELECT ENAME FROM EMP WHERE JOB<>'MANAGER';


+--------+
| ENAME |
+--------+
| KING |
| MARTIN |
| JAMES |
+--------+
3 rows in set (0.00 sec)

14. Display the names of employees starting with the letter K

mysql> SELECT ENAME FROM EMP WHERE ENAME LIKE'K%';


+-------+
| ENAME |
+-------+
| KING |
+-------+
1 row in set (0.00 sec)

15. Display the names of clerk and salesmen

mysql> SELECT ENAME FROM EMP WHERE JOB IN ( 'CLERK','SALESMAN');


+--------+
| ENAME |
+--------+
| MARTIN |
| JAMES |
+--------+
2 rows in set (0.00 sec)

16. Display the names of employees starting with the letter K and having 3 more
characters

mysql> SELECT ENAME FROM EMP WHERE ENAME LIKE 'K___';


+-------+
| ENAME |
+-------+
| KING |
+-------+
1 row in set (0.00 sec)

17. Display the names of employees which ends in G

4
mysql> SELECT ENAME FROM EMP WHERE ENAME LIKE ‘'%G';
+-------+
| ENAME |
+-------+
| KING |
+-------+
1 row in set (0.00 sec)

18. Display the Empno, name and department name of all employees

mysql> SELECT EMPNO, ENAME, DNAME FROM EMP,DEPT WHERE


EMP.DEPTNO=DEPT.DEPTNO;
+-------+--------+----------+
| EMPNO | ENAME | DNAME |
+-------+--------+----------+
| 7839 | KING | ACCOUNTS |
| 7698 | BLAKE | PROCESS |
| 7782 | CLARKE | ACCOUNTS |
| 7684 | MARTIN | PROCESS |
| 7900 | JAMES | ACCOUNTS |
+-------+--------+----------+
5 rows in set (0.00 sec)

19. Display the name salary of all employees who are at “New York”

mysql> SELECT ENAME, SAL FROM EMP,DEPT WHERE


EMP.DEPTNO=DEPT.DEPTNO AND LOCATION='NEWYORK';
+--------+------+
| ENAME | SAL |
+--------+------+
| BLAKE | 2850 |
| MARTIN | 1250 |
+--------+------+
2 rows in set (0.00 sec)

20. Display the name and hiredate of all employees in New York hired after 2012-01-01

mysql> SELECT ENAME,HIREDATE FROM EMP,DEPT WHERE


EMP.DEPTNO=DEPT.DEPTNO AND HIREDATE>'2012-01-01' AND
LOCATION='NEWYORK';
+--------+------------+
| ENAME | HIREDATE |
+--------+------------+
| MARTIN | 2012-09-01 |
+--------+------------+
1 row in set (0.00 sec)

5
21. Display the name and salary of employees in deptno 10 in descending order of salary

mysql> SELECT ENAME,SAL FROM EMP


-> WHERE DEPTNO=10
-> ORDER BY SAL DESC;
+--------+------+
| ENAME | SAL |
+--------+------+
| KING | 5000 |
| CLARKE | 2450 |
| JAMES | 950 |
+--------+------+
3 rows in set (0.06 sec)

22. Display the name and salary of employees in ascending order of salary

mysql> SELECT ENAME,SAL FROM EMP ORDER BY SAL;


+--------+------+
| ENAME | SAL |
+--------+------+
| JAMES | 950 |
| MARTIN | 1250 |
| CLARKE | 2450 |
| BLAKE | 2850 |
| KING | 5000 |
+--------+------+
5 rows in set (0.00 sec)

23. Display the names of employees in alphabetical order

mysql> SELECT ENAME FROM EMP ORDER BY ENAME;

+--------+
| ENAME |
+--------+
| BLAKE |
| CLARKE |
| JAMES |
| KING |
| MARTIN |
+--------+
4 rows in set (0.05 sec)

6
24. Display the no: of employees in each department

mysql> SELECT DEPTNO,COUNT(*) FROM EMP


-> GROUP BY DEPTNO;
+--------+----------+
| DEPTNO | COUNT(*) |
+--------+----------+
| 10 | 3|
| 30 | 2|
+--------+----------+
2 rows in set (0.02 sec)

25. Display the average salary of each job type

mysql> SELECT JOB, AVG(SAL) FROM EMP


-> GROUP BY JOB;
+-----------+-----------+
| JOB | AVG(SAL) |
+-----------+-----------+
| CLERK | 950.0000 |
| MANAGER | 2650.0000 |
| PRESIDENT | 5000.0000 |
| SALESMAN | 1250.0000 |
+-----------+-----------+
5 rows in set (0.00 sec)

26. Display the average salary of each job type only if the no: of employees in each type
is > 1

mysql> SELECT JOB, AVG(SAL) FROM EMP


-> GROUP BY JOB
-> HAVING COUNT(*) >1;
+---------+-----------+
| JOB | AVG(SAL) |
+---------+-----------+
| MANAGER | 2650.0000 |
+---------+-----------+
1 row in set (0.00 sec)

27. Display the maximum and minimum salary of each dept

mysql> SELECT DEPTNO,MAX(SAL), MIN(SAL) FROM EMP GROUP BY DEPTNO;

+--------+----------+----------+
| DEPTNO | MAX(SAL) | MIN(SAL) |
+--------+----------+----------+
| 10 | 5000 | 950 |
| 30 | 2850 | 1250 |

7
+--------+----------+----------+
2 rows in set (0.00 sec)

28. Increment the salary of all managers by 1000

mysql> UPDATE EMP SET SAL= SAL+1000 WHERE JOB = 'MANAGER';


Query OK, 2 rows affected (0.08 sec)
Rows matched: 2 Changed: 2 Warnings: 0

mysql> SELECT * FROM EMP;


+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
| 7839 | KING | PRESIDENT | NULL | 2010-11-01 | 5000 | NULL | 10 |
| 7698 | BLAKE | MANAGER | 7839 | 2010-12-01 | 3850 | NULL | 30 |
| 7782 | CLARKE | MANAGER | 7839 | 2010-01-01 | 3450 | NULL | 10 |
| 7684 | MARTIN | SALESMAN | 7698 | 2012-09-01 | 1250 | 400 | 30 |
| 7900 | JAMES | CLERK | 7782 | 2012-10-28 | 950 | NULL | 10 |
| 1111 | HARI | MANAGER | 7839 | 2010-01-01 | 3333 | NULL | 10 |
+-------+--------+-----------+------+------------+------+------+--------+
6 rows in set (0.00 sec)

You might also like