Mysql Subquery

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 5

balaji@mahendiren-smartail:~$ sudo mysql

[sudo] password for balaji:


Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database employee;


Query OK, 1 row affected (0.00 sec)

mysql> create table employees(employeeId int,firstName varchar(20),lastName varchar(20),email


varchar(20),phone_number long,hireDate date,jobId int,salary,managerId int,departmentId
int,primary key(employeeId));
ERROR 1046 (3D000): No database selected
mysql> use employee;
Database changed
mysql> create table employees(employeeId int,firstName varchar(20),lastName varchar(20),email
varchar(20),phone_number long,hireDate date,jobId int,salary,managerId int,departmentId
int,primary key(employeeId));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'managerId int,departmentId int,primary
key(employeeId))' at line 1
mysql> create table employees(employeeId int,firstName varchar(20),lastName varchar(20),email
varchar(20),phone_number long,hireDate date,jobId int,salary float(5),managerId int,departmentId
int,primary key(employeeId));
Query OK, 0 rows affected (0.02 sec)

mysql> create table


employees(employeeId,firstName,lastName,email,phone_number,hireDate,jobId,salary,managerId,
departmentId) values(1,"balaji","prasath","[email protected]",94872,'17-07-
2020',101,25000,11,01);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'firstName,lastName,email,phone_number,hireDate,jobId,salary,managerId,department' at line 1
mysql> insert into
employees(employeeId,firstName,lastName,email,phone_number,hireDate,jobId,salary,managerId,
departmentId) values(1,"balaji","prasath","[email protected]",94872,'17-07-
2020',101,25000,11,01);
ERROR 1292 (22007): Incorrect date value: '17-07-2020' for column 'hireDate' at row 1
mysql> insert into
employees(employeeId,firstName,lastName,email,phone_number,hireDate,jobId,salary,managerId,
departmentId) values(1,"balaji","prasath","[email protected]",94872,'2020-07-
17',101,25000,11,01);
Query OK, 1 row affected (0.00 sec)
mysql> insert into
employees(employeeId,firstName,lastName,email,phone_number,hireDate,jobId,salary,managerId,
departmentId) values(2,"ravi","kumar","[email protected]",948772,'2020-09-21',121,28000,12,02);
Query OK, 1 row affected (0.00 sec)

mysql> insert into


employees(employeeId,firstName,lastName,email,phone_number,hireDate,jobId,salary,managerId,
departmentId) values(3,"sunil","kumar","[email protected]",94889,'2020-11-21',131,36000,16,05);
Query OK, 1 row affected (0.00 sec)

mysql> select * from employees;


+------------+-----------+----------+---------------------+--------------+------------+-------+--------
+-----------+--------------+
| employeeId | firstName | lastName | email | phone_number | hireDate | jobId | salary |
managerId | departmentId |
+------------+-----------+----------+---------------------+--------------+------------+-------+--------
+-----------+--------------+
| 1 | balaji | prasath | [email protected] | 94872 | 2020-07-17 | 101 | 25000 | 11
| 1|
| 2 | ravi | kumar | [email protected] | 948772 | 2020-09-21 | 121 | 28000 | 12 |
2|
| 3 | sunil | kumar | [email protected] | 94889 | 2020-11-21 | 131 | 36000 | 16 |
5|
+------------+-----------+----------+---------------------+--------------+------------+-------+--------
+-----------+--------------+
3 rows in set (0.00 sec)

mysql> create table departemnts(departId int,departName varchar(20),locationId int,primary


key(departId));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into values (departId,departName,locationId) values (2,'production',1700);


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'values
(departId,departName,locationId) values (2,'production',1700)' at line 1
mysql> insert into departments (departId,departName,locationId) values (2,'production',1700);
ERROR 1146 (42S02): Table 'employee.departments' doesn't exist
mysql> insert into departemnts (departId,departName,locationId) values (2,'production',1700);
Query OK, 1 row affected (0.01 sec)

mysql> insert into departemnts (departId,departName,locationId) values (2,'production',


mysql> rename table departemnts to departments;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into departemnts (departId,departName,locationId) values (2,mysql> insert


int);departments (departId,departName,locationId) values (,'production',1700);
ERROR 1146 (42S02): Table 'employee.departemnts' doesn't exist
mysql> insert into departemnts (departId,departName,locationId) values (5,'backend',1700);
ERROR 1146 (42S02): Table 'employee.departemnts' doesn't exist
mysql> show tables;
+--------------------+
| Tables_in_employee |
+--------------------+
| departments |
| employees |
+--------------------+
2 rows in set (0.00 sec)

mysql> insert into departments (departId,departName,locationId) values (5,'backend',1700);


Query OK, 1 row affected (0.00 sec)

mysql> insert into departments (departId,departName,locationId) values (1,'frontend',1200);


Query OK, 1 row affected (0.00 sec)

mysql> select * from departments;


+----------+------------+------------+
| departId | departName | locationId |
+----------+------------+------------+
| 1 | frontend | 1200 |
| 2 | production | 1700 |
| 5 | backend | 1700 |
+----------+------------+------------+
3 rows in set (0.00 sec)

mysql> select 8 from departments where locationId=1700;


+---+
|8|
+---+
|8|
|8|
+---+
2 rows in set (0.00 sec)

mysql> select * from departments where locationId=1700;


+----------+------------+------------+
| departId | departName | locationId |
+----------+------------+------------+
| 2 | production | 1700 |
| 5 | backend | 1700 |
+----------+------------+------------+
2 rows in set (0.00 sec)

mysql> select * employeeId,firstName,lastName from employees where departId in(2,5) order by


firstName,lastName;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'employeeId,firstName,lastName from
employees where departId in(2,5) order by fir' at line 1
mysql> select * from employees;
+------------+-----------+----------+---------------------+--------------+------------+-------+--------
+-----------+--------------+
| employeeId | firstName | lastName | email | phone_number | hireDate | jobId | salary |
managerId | departmentId |
+------------+-----------+----------+---------------------+--------------+------------+-------+--------
+-----------+--------------+
| 1 | balaji | prasath | [email protected] | 94872 | 2020-07-17 | 101 | 25000 | 11
| 1|
| 2 | ravi | kumar | [email protected] | 948772 | 2020-09-21 | 121 | 28000 | 12 |
2|
| 3 | sunil | kumar | [email protected] | 94889 | 2020-11-21 | 131 | 36000 | 16 |
5|
+------------+-----------+----------+---------------------+--------------+------------+-------+--------
+-----------+--------------+
3 rows in set (0.00 sec)

mysql> select * employeeId,firstName,lastName from employees where departId in (2,5) order by


firstName,lastName;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'employeeId,firstName,lastName from
employees where departId in (2,5) order by fi' at line 1
mysql> select employeeId,firstName,lastName from employees where departId in (2,5) order by
firstName,lastName;
ERROR 1054 (42S22): Unknown column 'departId' in 'where clause'
mysql> mysql> select employeeId,firstName,lastName from employees where departmentId in
(2,5) order by firstName,lastName;
+------------+-----------+----------+
| employeeId | firstName | lastName |
+------------+-----------+----------+
| 2 | ravi | kumar |
| 3 | sunil | kumar |
+------------+-----------+----------+
2 rows in set (0.00 sec)

mysql> mysql> select employeeId,firstName,lastName from employees where departmen (2,5)


order by firstName,lastName;mysql> select employeeId,firstName,lastName from employees where
departId in (2,5) order by fir
mysql> select employeeId,firstName,lastName from employees where departmentId imysqelect
departId from departments eh Id in (2,5) order by firstName,lastName;mysql>
select employeeId,firstName,lastName from employees where departId in (2,5) order by
firstName,las
->
-> ;
ERROR 1054 (42S22): Unknown column 'departId' in 'where clause'
mysql> select employeeId,firstName,lastName from employees where departId in (semysql> lect
deemployeeId,firstName,lastName from employees where departId in (select departId from
departments where locatiomysql> select
->
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '' at line 1
mysql> select employeeId,firstName,lastName from employees where departmentId in(select
departId from departments where locationId=1700) order by firstName,lastName;
+------------+-----------+----------+
| employeeId | firstName | lastName |
+------------+-----------+----------+
| 2 | ravi | kumar |
| 3 | sunil | kumar |
+------------+-----------+----------+
2 rows in set (0.00 sec)

You might also like