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

notes-1

The document provides a comprehensive guide on working with databases and tables in SQL, including commands for creating, viewing, selecting, activating, and deleting databases. It details the creation and manipulation of tables, including inserting, updating, and deleting data, as well as the various SQL data types and commands. Additionally, it explains the differences between truncate, delete, and drop commands, along with examples of data manipulation and query language operations.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

notes-1

The document provides a comprehensive guide on working with databases and tables in SQL, including commands for creating, viewing, selecting, activating, and deleting databases. It details the creation and manipulation of tables, including inserting, updating, and deleting data, as well as the various SQL data types and commands. Additionally, it explains the differences between truncate, delete, and drop commands, along with examples of data manipulation and query language operations.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 35

working with databases

-----------------------
-> create a database
-> view the list of database
-> select database
-> activate database
-> delete database

mysql> create database db1;


Query OK, 1 row affected (0.02 sec)

mysql> create database db2;


Query OK, 1 row affected (0.01 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| db1 |
| db2 |
| information_schema |
| manualweekenddb9pm |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)

mysql> select database();


+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)

mysql> use db1;


Database changed
mysql> select database();
+------------+
| database() |
+------------+
| db1 |
+------------+
1 row in set (0.00 sec)

mysql> drop database db1;


Query OK, 0 rows affected (0.02 sec)

mysql> drop database db2;


Query OK, 0 rows affected (0.01 sec)
working with tables
-------------------

creating a table
description of tables
select the data from table
insert the data into the table
updating the data into the table
deleting the data from table

mysql> create table student(htno int, name varchar(10), email varchar(20));


Query OK, 0 rows affected (0.02 sec)

mysql> describe student;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| htno | int | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from student;


Empty set (0.01 sec)

mysql> insert into student values(1001, 'neha', '[email protected]');


Query OK, 1 row affected (0.01 sec)

mysql> insert into student values(1002, 'priya', '[email protected]');


Query OK, 1 row affected (0.01 sec)

mysql> insert into student values(1002, 'deepika', '[email protected]');


Query OK, 1 row affected (0.01 sec)

mysql> select * from student;


+------+---------+-------------------+
| htno | name | email |
+------+---------+-------------------+
| 1001 | neha | [email protected] |
| 1002 | priya | [email protected] |
| 1002 | deepika | [email protected] |
+------+---------+-------------------+
3 rows in set (0.00 sec)

mysql> select htno, name, email from student where htno=1001;


+------+------+----------------+
| htno | name | email |
+------+------+----------------+
| 1001 | neha | [email protected] |
+------+------+----------------+
1 row in set (0.00 sec)

mysql> select htno, name, email from student where htno=1002;


+------+---------+-------------------+
| htno | name | email |
+------+---------+-------------------+
| 1002 | priya | [email protected] |
| 1002 | deepika | [email protected] |
+------+---------+-------------------+
2 rows in set (0.00 sec)

mysql> insert into student(htno,name) values(1003, 'ajay');


Query OK, 1 row affected (0.01 sec)

mysql> insert into student(htno) values(1004);


Query OK, 1 row affected (0.01 sec)

mysql> update student set email='[email protected]' where htno=1003;


Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student;


+------+---------+-------------------+
| htno | name | email |
+------+---------+-------------------+
| 1001 | neha | [email protected] |
| 1002 | priya | [email protected] |
| 1002 | deepika | [email protected] |
| 1003 | ajay | [email protected] |
| 1004 | NULL | NULL |
+------+---------+-------------------+
5 rows in set (0.00 sec)

mysql> delete from student where htno=1004;


Query OK, 1 row affected (0.00 sec)

mysql> delete from student;


Query OK, 4 rows affected (0.00 sec)

mysql> select * from student;


Empty set (0.00 sec)

mysql> drop table student;


Query OK, 0 rows affected (0.01 sec)

Datatypes in SQL
----------------

1) Numeric data types


2) character datatypes
3) Date Data types
4) Large Object data type

1) Numeric data types


-------------------
Number - 123
Fraction - 67.123

Number:-
--------
-> tinyint -> 4 digit
-> smallint -> 5 digit
-> mediumint -> 9 digit
-> int -> 11 digit
-> bigint -> 20 digit

Fractions
---------

-> float(x,y) -> 24 decimal


-> double(x,y) -> 53 decimal
-> decimal(x,y) -> currency values

Character Data types


---------------------

char(n) -> n character


varchar(n) -> n character
tinytext(n)
text(n)
mediumtext(n)
longtext(n)

Date data type


--------------

date -> 'YYYY-MM-DD'


datetime -> 'YYYY-MM-DD HH:MM:SS'
time -> 'HH:MM:SS'
year -> 'YYYY'

Large Object Data type


---------------------
document
pdf
resume doc
photo
jpg
mp3
mp4

TINYBLOB -> 500MB


MEDIUMBLOB -> 2GB
BLOB -> 5 GB

SQL COMMANDS
-------------

-> DDL (Data Definition language) - create, alter, rename, drop,


truncate

-> DML (Data Manipulation Language) - insert update delete

-> DQL (Data Query Language) -> select

-> DCL(Data Control Langauge) - grant revoke

-> TCL -> Transaction Control langauge - Commit, rollback, savepoint

-> DDL (Data Definition language) - create, alter, rename, truncate


-------------------------------------------------------------------

mysql> create table student(name varchar(9), mobile char(10), percentage


double(4,2));

adding the column (grade)


--------------------------

mysql> alter table student add column grade char(1);


Query OK, 0 rows affected (0.02 sec)

mysql> desc student;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| name | varchar(9) | YES | | NULL | |
| mobile | char(10) | YES | | NULL | |
| percentage | double(4,2) | YES | | NULL | |
| grade | char(1) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into student(name, mobile, percentage) values('neha', '49827398',


68.6
7);
Query OK, 1 row affected (0.00 sec)

mysql> insert into student(name, mobile, percentage) values('ajay', '1234567',


65.67
);
Query OK, 1 row affected (0.00 sec)

mysql> insert into student(name, mobile, percentage) values('priya', '6796897',


65.6
7);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;


+-------+----------+------------+-------+
| name | mobile | percentage | grade |
+-------+----------+------------+-------+
| neha | 49827398 | 68.67 | NULL |
| ajay | 1234567 | 65.67 | NULL |
| priya | 6796897 | 65.67 | NULL |
+-------+----------+------------+-------+
3 rows in set (0.00 sec)

mysql> alter table student add column sid tinyint first;


Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from student;


+------+-------+----------+------------+-------+
| sid | name | mobile | percentage | grade |
+------+-------+----------+------------+-------+
| NULL | neha | 49827398 | 68.67 | NULL |
| NULL | ajay | 1234567 | 65.67 | NULL |
| NULL | priya | 6796897 | 65.67 | NULL |
+------+-------+----------+------------+-------+
3 rows in set (0.00 sec)

I want to modify the existing column grade(char(4))


----------------------------------------------------

mysql> alter table student modify column grade char(4);


Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0

drop the column


---------------

mysql> alter table student drop column mname;


Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from student;


+------+-------+----------+------------+-------+
| sid | name | mobile | percentage | grade |
+------+-------+----------+------------+-------+
| NULL | neha | 49827398 | 68.67 | NULL |
| NULL | ajay | 1234567 | 65.67 | NULL |
| NULL | priya | 6796897 | 65.67 | NULL |
+------+-------+----------+------------+-------+
3 rows in set (0.00 sec)

truncate
---------

-> remove total content present in table.


-> it also remove all the db objects on the table.
-> we cant roll back the data.

mysql> truncate demo; -- all table content will be removed.

rename
-------

-> It is used change the name of table;

mysql> rename table demo to employee;


Query OK, 0 rows affected (0.01 sec)

mysql> show tables;


+------------------+
| Tables_in_javadb |
+------------------+
| employee |
+------------------+
1 row in set (0.00 sec)

what is difference between truncate delete and drop command

truncate
---------
DDL
truncate db1;
delete data NOT STRUCTURE
all rows
no rollback

delete
-------
DML
delete from db1;
delete data NOT STRUCTURE
specific / all rows
rollback

drop
-----

DDL
drop table demo;;
delete data & STRUCTURE
specific / all rows
all constrainst
all indexes
all privileges
no rollback
DML Commands
------------
insert
update
delete

insert
-------

mysql> insert into student values(1, 'priya', 'java', 89);


Query OK, 1 row affected (0.01 sec)

mysql> insert into student values(2, 'ankit', 'db', 87);


Query OK, 1 row affected (0.01 sec)

mysql> insert into student values(3, 'fouzia', 'python', 99);


Query OK, 1 row affected (0.00 sec)

mysql> select * from student;


+------+--------+--------+-------+
| sid | name | course | marks |
+------+--------+--------+-------+
| 1 | priya | java | 89 |
| 2 | ankit | db | 87 |
| 3 | fouzia | python | 99 |
+------+--------+--------+-------+

mysql> insert into student(sid, name,course) values(4, 'venkat', 'dot net');


Query OK, 1 row affected (0.01 sec)

mysql> select * from student;


+------+--------+---------+-------+
| sid | name | course | marks |
+------+--------+---------+-------+
| 1 | priya | java | 89 |
| 2 | ankit | db | 87 |
| 3 | fouzia | python | 99 |
| 4 | venkat | dot net | NULL |
+------+--------+---------+-------+

mysql> insert into student values(6, 'deepika', 'DB TESTING', 79), (7, 'mahesh',
'Oracle', 67);
Query OK, 2 rows affected (0.01 sec)

update
-------

mysql> update student set marks = marks+5;


Query OK, 6 rows affected (0.01 sec)

mysql> update student set marks = marks+5 where name='priya';


Query OK, 1 row affected (0.01 sec)

mysql> update student set marks = marks+5 where sid=6;


Query OK, 1 row affected (0.00 sec)

delete
--------

mysql> delete from student where sid=7;


Query OK, 1 row affected (0.01 sec)

mysql> select * from student;


+------+---------+------------+-------+
| sid | name | course | marks |
+------+---------+------------+-------+
| 1 | priya | java | 99 |
| 2 | ankit | db | 92 |
| 3 | fouzia | python | 104 |
| 4 | venkat | dot net | 77 |
| 5 | neha | aws | 83 |
| 6 | deepika | DB TESTING | 89 |
+------+---------+------------+-------+
6 rows in set (0.00 sec)

mysql> delete from student where course='aws';


Query OK, 1 row affected (0.01 sec)

mysql> select * from student;


+------+---------+------------+-------+
| sid | name | course | marks |
+------+---------+------------+-------+
| 1 | priya | java | 99 |
| 2 | ankit | db | 92 |
| 3 | fouzia | python | 104 |
| 4 | venkat | dot net | 77 |
| 6 | deepika | DB TESTING | 89 |
+------+---------+------------+-------+
5 rows in set (0.00 sec)

mysql> delete from student;


Query OK, 5 rows affected (0.00 sec)

mysql> select * from student;


Empty set (0.00 sec)

DQL (Data Query Language)


-------------------------
select command

mysql> select * from myemp limit 10;

mysql> select FIRST_NAME , LAST_NAME, SALARY from myemp limit 10;


+------------+-----------+----------+
| FIRST_NAME | LAST_NAME | SALARY |
+------------+-----------+----------+
| Steven | King | 24000.00 |
| Neena | Kochhar | 17000.00 |
| Lex | De Haan | 17000.00 |
| Alexander | Hunold | 9000.00 |
| Bruce | Ernst | 6000.00 |
| David | Austin | 4800.00 |
| Valli | Pataballa | 4800.00 |
| Diana | Lorentz | 4200.00 |
| Nancy | Greenberg | 12000.00 |
| Daniel | Faviet | 9000.00 |
+------------+-----------+----------+

mysql> select FIRST_NAME , SALARY , salary*0.02 as bonus , salary+salary*0.02 as


totalsalary from myemp limit 10;
+------------+----------+----------+-------------+
| FIRST_NAME | SALARY | bonus | totalsalary |
+------------+----------+----------+-------------+
| Steven | 24000.00 | 480.0000 | 24480.0000 |
| Neena | 17000.00 | 340.0000 | 17340.0000 |
| Lex | 17000.00 | 340.0000 | 17340.0000 |
| Alexander | 9000.00 | 180.0000 | 9180.0000 |
| Bruce | 6000.00 | 120.0000 | 6120.0000 |
| David | 4800.00 | 96.0000 | 4896.0000 |
| Valli | 4800.00 | 96.0000 | 4896.0000 |
| Diana | 4200.00 | 84.0000 | 4284.0000 |
| Nancy | 12000.00 | 240.0000 | 12240.0000 |
| Daniel | 9000.00 | 180.0000 | 9180.0000 |
+------------+----------+----------+-------------+

salary*0.02 as bonus -> Here bonus column is an alias name

salary+salary*0.02 as totalsalary -> Here totalsalary column is an alias name

=========================================================================

mysql> select distinct dep_id from myemp;

mysql> select distinct JOB_ID from myemp;

mysql> select distinct MGR_ID , DEP_ID from myemp;

mysql> select * from myemp order by dep_id desc limit 10;

mysql> select * from myemp order by dep_id, HIRE_DATE desc limit 10;

mysql> select * from myemp where HIRE_DATE > '2000-12-31';


+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY |
COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
| 140 | Joshua | Patel | JPATEL | 2011-07-27 | ST_CLERK | 2500.00 |
0.00 | 123 | 50 |
| 146 | Karen | Partners | KPARTNER | 2019-08-02 | SA_MAN | 13500.00 |
0.30 | 100 | 80 |
| 148 | Gerald | Cambrault | GCAMBRAU | 2015-08-04 | SA_MAN | 11000.00 |
0.30 | 100 | 80 |
| 151 | David | Bernstein | DBERNSTE | 2013-08-07 | SA_REP | 9500.00 |
0.25 | 145 | 80 |
| 157 | Patrick | Sully | PSULLY | 2002-08-13 | SA_REP | 9500.00 |
0.35 | 146 | 80 |
| 160 | Louise | Doran | LDORAN | 2003-08-16 | SA_REP | 7500.00 |
0.30 | 146 | 80 |
| 164 | Mattea | Marvins | MMARVINS | 2007-08-20 | SA_REP | 7200.00 |
0.10 | 147 | 80 |
| 184 | Nandita | Sarchand | NSARCHAN | 2012-09-09 | SH_CLERK | 4200.00 |
0.00 | 121 | 50 |
| 187 | Anthony | Cabrio | ACABRIO | 2007-09-12 | SH_CLERK | 3000.00 |
0.00 | 121 | 50 |
+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
9 rows in set (0.00 sec)

selecting the data based on some condition (where by clause)


------------------------------------------------------------

mysql> select * from emp;


+------+---------+--------+
| id | name | salary |
+------+---------+--------+
| 1 | anu | 4000 |
| 21 | priya | 4500 |
| 3 | kumar | 4600 |
| 4 | durgesh | 3400 |
| 5 | lokesh | 2000 |
| 6 | avanish | 26000 |
| 7 | hari | 2800 |
| 8 | john | 9000 |
| 9 | akil | 9000 |
| 10 | suresh | 11000 |
| 11 | satya | 12000 |
| 12 | venkat | NULL |
+------+---------+--------+
12 rows in set (0.00 sec)

whereby clause - operators

=
<
<=
>
>=
<> or !=
between
and
or
not
in
not in
is
is not
like

mysql> select id , name from emp where id=6;


+------+---------+
| id | name |
+------+---------+
| 6 | avanish |
+------+---------+
1 row in set (0.00 sec)

mysql> select * from emp where salary<4000;


+------+---------+--------+
| id | name | salary |
+------+---------+--------+
| 4 | durgesh | 3400 |
| 5 | lokesh | 2000 |
| 7 | hari | 2800 |
+------+---------+--------+
3 rows in set (0.00 sec)

mysql> select id , name from emp where salary>=4000;


+------+---------+
| id | name |
+------+---------+
| 1 | anu |
| 21 | priya |
| 3 | kumar |
| 6 | avanish |
| 8 | john |
| 9 | akil |
| 10 | suresh |
| 11 | satya |
+------+---------+
8 rows in set (0.00 sec)

mysql> select * from emp where salary>=3000 and salary<=7000;


+------+---------+--------+
| id | name | salary |
+------+---------+--------+
| 1 | anu | 4000 |
| 21 | priya | 4500 |
| 3 | kumar | 4600 |
| 4 | durgesh | 3400 |
+------+---------+--------+
4 rows in set (0.00 sec)

mysql> select * from emp where id !=1;


+------+---------+--------+
| id | name | salary |
+------+---------+--------+
| 21 | priya | 4500 |
| 3 | kumar | 4600 |
| 4 | durgesh | 3400 |
| 5 | lokesh | 2000 |
| 6 | avanish | 26000 |
| 7 | hari | 2800 |
| 8 | john | 9000 |
| 9 | akil | 9000 |
| 10 | suresh | 11000 |
| 11 | satya | 12000 |
| 12 | venkat | NULL |
+------+---------+--------+
11 rows in set (0.00 sec)

mysql> select * from emp where id <> 1;


+------+---------+--------+
| id | name | salary |
+------+---------+--------+
| 21 | priya | 4500 |
| 3 | kumar | 4600 |
| 4 | durgesh | 3400 |
| 5 | lokesh | 2000 |
| 6 | avanish | 26000 |
| 7 | hari | 2800 |
| 8 | john | 9000 |
| 9 | akil | 9000 |
| 10 | suresh | 11000 |
| 11 | satya | 12000 |
| 12 | venkat | NULL |
+------+---------+--------+
11 rows in set (0.00 sec)

mysql> select * from emp where salary is not null;


+------+---------+--------+
| id | name | salary |
+------+---------+--------+
| 1 | anu | 4000 |
| 21 | priya | 4500 |
| 3 | kumar | 4600 |
| 4 | durgesh | 3400 |
| 5 | lokesh | 2000 |
| 6 | avanish | 26000 |
| 7 | hari | 2800 |
| 8 | john | 9000 |
| 9 | akil | 9000 |
| 10 | suresh | 11000 |
| 11 | satya | 12000 |
+------+---------+--------+
11 rows in set (0.00 sec)

like operator
-------------
=> pattern matching
=> special character
=> case is not important;

% -> any number opf character including 0.

_ -> matches exactly one character.

mysql> select * from myemp where first_name like 'a%';


+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY |
COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
| 103 | Alexander | Hunold | AHUNOLD | 1976-06-20 | IT_PROG | 9000.00 |
0.00 | 102 | 60 |
| 115 | Alexander | Khoo | AKHOO | 1983-07-02 | PU_CLERK | 3100.00 |
0.00 | 114 | 30 |
| 121 | Adam | Fripp | AFRIPP | 1990-07-08 | ST_MAN | 8200.00 |
0.00 | 100 | 50 |
| 147 | Alberto | Errazuriz | AERRAZUR | 1987-08-03 | SA_MAN | 12000.00 |
0.30 | 100 | 80 |
| 158 | Allan | McEwen | AMCEWEN | 1987-08-14 | SA_REP | 9000.00 |
0.35 | 146 | 80 |
| 167 | Amit | Banda | ABANDA | 1987-08-23 | SA_REP | 6200.00 |
0.10 | 147 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 1996-08-31 | SA_REP | 8800.00 |
0.25 | 149 | 80 |
| 185 | Alexis | Bull | ABULL | 1987-09-10 | SH_CLERK | 4100.00 |
0.00 | 121 | 50 |
| 187 | Anthony | Cabrio | ACABRIO | 2007-09-12 | SH_CLERK | 3000.00 |
0.00 | 121 | 50 |
| 196 | Alana | Walsh | AWALSH | 1987-09-21 | SH_CLERK | 3100.00 |
0.00 | 124 | 50 |
+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+

mysql> select * from myemp where first_name like '%a';


+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY |
COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+
| 101 | Neena | Kochhar | NKOCHHAR | 1987-06-18 | AD_VP | 17000.00 |
0.00 | 100 | 90 |
| 107 | Diana | Lorentz | DLORENTZ | 1987-06-24 | IT_PROG | 4200.00 |
0.00 | 103 | 60 |
| 123 | Shanta | Vollman | SVOLLMAN | 1987-07-10 | ST_MAN | 6500.00 |
0.00 | 100 | 50 |
| 125 | Julia | Nayer | JNAYER | 1987-07-12 | ST_CLERK | 3200.00 |
0.00 | 120 | 50 |
| 129 | Laura | Bissot | LBISSOT | 1987-07-16 | ST_CLERK | 3300.00 |
0.00 | 121 | 50 |
| 140 | Joshua | Patel | JPATEL | 2011-07-27 | ST_CLERK | 2500.00 |
0.00 | 123 | 50 |
| 141 | Trenna | Rajs | TRAJS | 1987-07-28 | ST_CLERK | 3500.00 |
0.00 | 124 | 50 |
| 162 | Clara | Vishney | CVISHNEY | 1987-08-18 | SA_REP | 10500.00 |
0.25 | 147 | 80 |
| 164 | Mattea | Marvins | MMARVINS | 2007-08-20 | SA_REP | 7200.00 |
0.10 | 147 | 80 |
| 168 | Lisa | Ozer | LOZER | 1987-08-24 | SA_REP | 11500.00 |
0.25 | 148 | 80 |
| 173 | Sundita | Kumar | SKUMAR | 1987-08-29 | SA_REP | 6100.00 |
0.10 | 148 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 1996-08-31 | SA_REP | 8800.00 |
0.25 | 149 | 80 |
| 182 | Martha | Sullivan | MSULLIVA | 1987-09-07 | SH_CLERK | 2500.00 |
0.00 | 120 | 50 |
| 184 | Nandita | Sarchand | NSARCHAN | 2012-09-09 | SH_CLERK | 4200.00 |
0.00 | 121 | 50 |
| 186 | Julia | Dellinger | JDELLING | 1987-09-11 | SH_CLERK | 3400.00 |
0.00 | 121 | 50 |
| 196 | Alana | Walsh | AWALSH | 1987-09-21 | SH_CLERK | 3100.00 |
0.00 | 124 | 50 |
+--------+------------+-----------+----------+------------+----------+----------
+----------------+--------+--------+

mysql> select * from myemp where first_name like '%a%';


+--------+-------------+------------+----------+------------+------------
+----------+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY
| COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+-------------+------------+----------+------------+------------
+----------+----------------+--------+--------+
| 101 | Neena | Kochhar | NKOCHHAR | 1987-06-18 | AD_VP | 17000.00
| 0.00 | 100 | 90 |
| 103 | Alexander | Hunold | AHUNOLD | 1976-06-20 | IT_PROG | 9000.00
| 0.00 | 102 | 60 |
| 105 | David | Austin | DAUSTIN | 1987-06-22 | IT_PROG | 4800.00
| 0.00 | 103 | 60 |
| 106 | Valli | Pataballa | VPATABAL | 1980-06-23 | IT_PROG | 4800.00
| 0.00 | 103 | 60 |
| 107 | Diana | Lorentz | DLORENTZ | 1987-06-24 | IT_PROG | 4200.00
| 0.00 | 103 | 60 |
| 108 | Nancy | Greenberg | NGREENBE | 1987-06-25 | FI_MGR | 12000.00
| 0.00 | 101 | 100 |
| 109 | Daniel | Faviet | DFAVIET | 1992-06-26 | FI_ACCOUNT | 9000.00
| 0.00 | 108 | 100 |
| 111 | Ismael | Sciarra | ISCIARRA | 1987-06-28 | FI_ACCOUNT | 7700.00
| 0.00 | 108 | 100 |
| 112 | Jose Manuel | Urman | JMURMAN | 1981-06-29 | FI_ACCOUNT | 7800.00
| 0.00 | 108 | 100 |
| 115 | Alexander | Khoo | AKHOO | 1983-07-02 | PU_CLERK | 3100.00
| 0.00 | 114 | 30 |
| 117 | Sigal | Tobias | STOBIAS | 1987-07-04 | PU_CLERK | 2800.00
| 0.00 | 114 | 30 |
| 119 | Karen | Colmenares | KCOLMENA | 1987-07-06 | PU_CLERK | 2500.00
| 0.00 | 114 | 30 |
| 120 | Matthew | Weiss | MWEISS | 1987-07-07 | ST_MAN | 8000.00
| 0.00 | 100 | 50 |
| 121 | Adam | Fripp | AFRIPP | 1990-07-08 | ST_MAN | 8200.00
| 0.00 | 100 | 50 |
| 122 | Payam | Kaufling | PKAUFLIN | 1987-07-09 | ST_MAN | 7900.00
| 0.00 | 100 | 50 |
| 123 | Shanta | Vollman | SVOLLMAN | 1987-07-10 | ST_MAN | 6500.00
| 0.00 | 100 | 50 |
| 125 | Julia | Nayer | JNAYER | 1987-07-12 | ST_CLERK | 3200.00
| 0.00 | 120 | 50 |
| 127 | James | Landry | JLANDRY | 1978-07-14 | ST_CLERK | 2400.00
| 0.00 | 120 | 50 |
| 129 | Laura | Bissot | LBISSOT | 1987-07-16 | ST_CLERK | 3300.00
| 0.00 | 121 | 50 |
| 131 | James | Marlow | JAMRLOW | 1987-07-18 | ST_CLERK | 2500.00
| 0.00 | 121 | 50 |
| 133 | Jason | Mallin | JMALLIN | 1985-07-20 | ST_CLERK | 3300.00
| 0.00 | 122 | 50 |
| 134 | Michael | Rogers | MROGERS | 1987-07-21 | ST_CLERK | 2900.00
| 0.00 | 122 | 50 |
| 136 | Hazel | Philtanker | HPHILTAN | 1990-07-23 | ST_CLERK | 2200.00
| 0.00 | 122 | 50 |
| 140 | Joshua | Patel | JPATEL | 2011-07-27 | ST_CLERK | 2500.00
| 0.00 | 123 | 50 |
| 141 | Trenna | Rajs | TRAJS | 1987-07-28 | ST_CLERK | 3500.00
| 0.00 | 124 | 50 |
| 143 | Randall | Matos | RMATOS | 1987-07-30 | ST_CLERK | 2600.00
| 0.00 | 124 | 50 |
| 146 | Karen | Partners | KPARTNER | 2019-08-02 | SA_MAN | 13500.00
| 0.30 | 100 | 80 |
| 147 | Alberto | Errazuriz | AERRAZUR | 1987-08-03 | SA_MAN | 12000.00
| 0.30 | 100 | 80 |
| 148 | Gerald | Cambrault | GCAMBRAU | 2015-08-04 | SA_MAN | 11000.00
| 0.30 | 100 | 80 |
| 151 | David | Bernstein | DBERNSTE | 2013-08-07 | SA_REP | 9500.00
| 0.25 | 145 | 80 |
| 154 | Nanette | Cambrault | NCAMBRAU | 1987-08-10 | SA_REP | 7500.00
| 0.20 | 145 | 80 |
| 156 | Janette | King | JKING | 1987-08-12 | SA_REP | 10000.00
| 0.35 | 146 | 80 |
| 157 | Patrick | Sully | PSULLY | 2002-08-13 | SA_REP | 9500.00
| 0.35 | 146 | 80 |
| 158 | Allan | McEwen | AMCEWEN | 1987-08-14 | SA_REP | 9000.00
| 0.35 | 146 | 80 |
| 161 | Sarath | Sewall | SSEWALL | 1987-08-17 | SA_REP | 7000.00
| 0.25 | 146 | 80 |
| 162 | Clara | Vishney | CVISHNEY | 1987-08-18 | SA_REP | 10500.00
| 0.25 | 147 | 80 |
| 163 | Danielle | Greene | DGREENE | 1987-08-19 | SA_REP | 9500.00
| 0.15 | 147 | 80 |
| 164 | Mattea | Marvins | MMARVINS | 2007-08-20 | SA_REP | 7200.00
| 0.10 | 147 | 80 |
| 165 | David | Lee | DLEE | 1987-08-21 | SA_REP | 6800.00
| 0.10 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 1987-08-22 | SA_REP | 6400.00
| 0.10 | 147 | 80 |
| 167 | Amit | Banda | ABANDA | 1987-08-23 | SA_REP | 6200.00
| 0.10 | 147 | 80 |
| 168 | Lisa | Ozer | LOZER | 1987-08-24 | SA_REP | 11500.00
| 0.25 | 148 | 80 |
| 169 | Harrison | Bloom | HBLOOM | 1999-08-25 | SA_REP | 10000.00
| 0.20 | 148 | 80 |
| 170 | Tayler | Fox | TFOX | 1987-08-26 | SA_REP | 9600.00
| 0.20 | 148 | 80 |
| 171 | William | Smith | WSMITH | 1987-08-27 | SA_REP | 7400.00
| 0.15 | 148 | 80 |
| 172 | Elizabeth | Bates | EBATES | 1987-08-28 | SA_REP | 7300.00
| 0.15 | 148 | 80 |
| 173 | Sundita | Kumar | SKUMAR | 1987-08-29 | SA_REP | 6100.00
| 0.10 | 148 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 1996-08-31 | SA_REP | 8800.00
| 0.25 | 149 | 80 |
| 176 | Jonathon | Taylor | JTAYLOR | 1987-09-01 | SA_REP | 8600.00
| 0.20 | 149 | 80 |
| 177 | Jack | Livingston | JLIVINGS | 1987-09-02 | SA_REP | 8400.00
| 0.20 | 149 | 80 |
| 179 | Charles | Johnson | CJOHNSON | 1999-09-04 | SA_REP | 6200.00
| 0.10 | 149 | 80 |
| 181 | Jean | Fleaur | JFLEAUR | 1987-09-06 | SH_CLERK | 3100.00
| 0.00 | 120 | 50 |
| 182 | Martha | Sullivan | MSULLIVA | 1987-09-07 | SH_CLERK | 2500.00
| 0.00 | 120 | 50 |
| 183 | Girard | Geoni | GGEONI | 1987-09-08 | SH_CLERK | 2800.00
| 0.00 | 120 | 50 |
| 184 | Nandita | Sarchand | NSARCHAN | 2012-09-09 | SH_CLERK | 4200.00
| 0.00 | 121 | 50 |
| 185 | Alexis | Bull | ABULL | 1987-09-10 | SH_CLERK | 4100.00
| 0.00 | 121 | 50 |
| 186 | Julia | Dellinger | JDELLING | 1987-09-11 | SH_CLERK | 3400.00
| 0.00 | 121 | 50 |
| 187 | Anthony | Cabrio | ACABRIO | 2007-09-12 | SH_CLERK | 3000.00
| 0.00 | 121 | 50 |
| 191 | Randall | Perkins | RPERKINS | 1987-09-16 | SH_CLERK | 2500.00
| 0.00 | 122 | 50 |
| 192 | Sarah | Bell | SBELL | 1987-09-17 | SH_CLERK | 4000.00
| 0.00 | 123 | 50 |
| 194 | Samuel | McCain | SMCCAIN | 2000-09-19 | SH_CLERK | 3200.00
| 0.00 | 123 | 50 |
| 195 | Vance | Jones | VJONES | 1996-09-20 | SH_CLERK | 2800.00
| 0.00 | 123 | 50 |
| 196 | Alana | Walsh | AWALSH | 1987-09-21 | SH_CLERK | 3100.00
| 0.00 | 124 | 50 |
| 198 | Donald | OConnell | DOCONNEL | 1997-09-23 | SH_CLERK | 2600.00
| 0.00 | 124 | 50 |
| 199 | Douglas | Grant | DGRANT | 1987-09-24 | SH_CLERK | 2600.00
| 0.00 | 124 | 50 |
| 201 | Michael | Hartstein | MHARTSTE | 1987-09-26 | MK_MAN | 13000.00
| 0.00 | 100 | 20 |
| 202 | Pat | Fay | PFAY | 1987-09-27 | MK_REP | 6000.00
| 0.00 | 201 | 20 |
| 203 | Susan | Mavris | SMAVRIS | 1989-09-28 | HR_REP | 6500.00
| 0.00 | 101 | 40 |
| 204 | Hermann | Baer | HBAER | 1987-09-29 | PR_REP | 10000.00
| 0.00 | 101 | 70 |
| 206 | William | Gietz | WGIETZ | 1997-10-01 | AC_ACCOUNT | 8300.00
| 0.00 | 205 | 110 |
+--------+-------------+------------+----------+------------+------------
+----------+----------------+--------+--------+

mysql> select * from myemp where first_name like '_____';


+--------+------------+-------------+----------+------------+----------+----------
+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY |
COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+------------+-------------+----------+------------+----------+----------
+----------------+--------+--------+
| 101 | Neena | Kochhar | NKOCHHAR | 1987-06-18 | AD_VP | 17000.00 |
0.00 | 100 | 90 |
| 104 | Bruce | Ernst | BERNST | 1987-06-21 | IT_PROG | 6000.00 |
0.00 | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 1987-06-22 | IT_PROG | 4800.00 |
0.00 | 103 | 60 |
| 106 | Valli | Pataballa | VPATABAL | 1980-06-23 | IT_PROG | 4800.00 |
0.00 | 103 | 60 |
| 107 | Diana | Lorentz | DLORENTZ | 1987-06-24 | IT_PROG | 4200.00 |
0.00 | 103 | 60 |
| 108 | Nancy | Greenberg | NGREENBE | 1987-06-25 | FI_MGR | 12000.00 |
0.00 | 101 | 100 |
| 117 | Sigal | Tobias | STOBIAS | 1987-07-04 | PU_CLERK | 2800.00 |
0.00 | 114 | 30 |
| 119 | Karen | Colmenares | KCOLMENA | 1987-07-06 | PU_CLERK | 2500.00 |
0.00 | 114 | 30 |
| 122 | Payam | Kaufling | PKAUFLIN | 1987-07-09 | ST_MAN | 7900.00 |
0.00 | 100 | 50 |
| 124 | Kevin | Mourgos | KMOURGOS | 1997-07-11 | ST_MAN | 5800.00 |
0.00 | 100 | 50 |
| 125 | Julia | Nayer | JNAYER | 1987-07-12 | ST_CLERK | 3200.00 |
0.00 | 120 | 50 |
| 126 | Irene | Mikkilineni | IMIKKILI | 1987-07-13 | ST_CLERK | 2700.00 |
0.00 | 120 | 50 |
| 127 | James | Landry | JLANDRY | 1978-07-14 | ST_CLERK | 2400.00 |
0.00 | 120 | 50 |
| 129 | Laura | Bissot | LBISSOT | 1987-07-16 | ST_CLERK | 3300.00 |
0.00 | 121 | 50 |
| 130 | Mozhe | Atkinson | MATKINSO | 1983-07-17 | ST_CLERK | 2800.00 |
0.00 | 121 | 50 |
| 131 | James | Marlow | JAMRLOW | 1987-07-18 | ST_CLERK | 2500.00 |
0.00 | 121 | 50 |
| 133 | Jason | Mallin | JMALLIN | 1985-07-20 | ST_CLERK | 3300.00 |
0.00 | 122 | 50 |
| 136 | Hazel | Philtanker | HPHILTAN | 1990-07-23 | ST_CLERK | 2200.00 |
0.00 | 122 | 50 |
| 144 | Peter | Vargas | PVARGAS | 1998-07-31 | ST_CLERK | 2500.00 |
0.00 | 124 | 50 |
| 146 | Karen | Partners | KPARTNER | 2019-08-02 | SA_MAN | 13500.00 |
0.30 | 100 | 80 |
| 149 | Eleni | Zlotkey | EZLOTKEY | 1987-08-05 | SA_MAN | 10500.00 |
0.20 | 100 | 80 |
| 150 | Peter | Tucker | PTUCKER | 1987-08-06 | SA_REP | 10000.00 |
0.30 | 145 | 80 |
| 151 | David | Bernstein | DBERNSTE | 2013-08-07 | SA_REP | 9500.00 |
0.25 | 145 | 80 |
| 152 | Peter | Hall | PHALL | 1987-08-08 | SA_REP | 9000.00 |
0.25 | 145 | 80 |
| 158 | Allan | McEwen | AMCEWEN | 1987-08-14 | SA_REP | 9000.00 |
0.35 | 146 | 80 |
| 162 | Clara | Vishney | CVISHNEY | 1987-08-18 | SA_REP | 10500.00 |
0.25 | 147 | 80 |
| 165 | David | Lee | DLEE | 1987-08-21 | SA_REP | 6800.00 |
0.10 | 147 | 80 |
| 174 | Ellen | Abel | EABEL | 1987-08-30 | SA_REP | 11000.00 |
0.30 | 149 | 80 |
| 186 | Julia | Dellinger | JDELLING | 1987-09-11 | SH_CLERK | 3400.00 |
0.00 | 121 | 50 |
| 188 | Kelly | Chung | KCHUNG | 1987-09-13 | SH_CLERK | 3800.00 |
0.00 | 122 | 50 |
| 192 | Sarah | Bell | SBELL | 1987-09-17 | SH_CLERK | 4000.00 |
0.00 | 123 | 50 |
| 195 | Vance | Jones | VJONES | 1996-09-20 | SH_CLERK | 2800.00 |
0.00 | 123 | 50 |
| 196 | Alana | Walsh | AWALSH | 1987-09-21 | SH_CLERK | 3100.00 |
0.00 | 124 | 50 |
| 197 | Kevin | Feeney | KFEENEY | 1987-09-22 | SH_CLERK | 3000.00 |
0.00 | 124 | 50 |
| 203 | Susan | Mavris | SMAVRIS | 1989-09-28 | HR_REP | 6500.00 |
0.00 | 101 | 40 |
+--------+------------+-------------+----------+------------+----------+----------
+----------------+--------+--------+

sorting record | order by clause


----------------------------------

ascending order [default]


descending order
It is possible to order by more than one column.

mysql> select * from myemp order by dep_id;


+--------+-------------+-------------+----------+------------+------------
+----------+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY
| COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+-------------+-------------+----------+------------+------------
+----------+----------------+--------+--------+
| 200 | Jennifer | Whalen | JWHALEN | 1988-09-25 | AD_ASST |
4400.00 | 0.00 | 101 | 10 |
| 201 | Michael | Hartstein | MHARTSTE | 1987-09-26 | MK_MAN |
13000.00 | 0.00 | 100 | 20 |
| 202 | Pat | Fay | PFAY | 1987-09-27 | MK_REP |
6000.00 | 0.00 | 201 | 20 |
| 114 | Den | Raphaely | DRAPHEAL | 1987-07-01 | PU_MAN |
11000.00 | 0.00 | 100 | 30 |
| 115 | Alexander | Khoo | AKHOO | 1983-07-02 | PU_CLERK |
3100.00 | 0.00 | 114 | 30 |
| 116 | Shelli | Baida | SBAIDA | 1987-07-03 | PU_CLERK |
2900.00 | 0.00 | 114 | 30 |
| 117 | Sigal | Tobias | STOBIAS | 1987-07-04 | PU_CLERK |
2800.00 | 0.00 | 114 | 30 |
| 118 | Guy | Himuro | GHIMURO | 1995-07-05 | PU_CLERK |
2600.00 | 0.00 | 114 | 30 |
| 119 | Karen | Colmenares | KCOLMENA | 1987-07-06 | PU_CLERK |
2500.00 | 0.00 | 114 | 30 |
| 203 | Susan | Mavris | SMAVRIS | 1989-09-28 | HR_REP |
6500.00 | 0.00 | 101 | 40 |
| 120 | Matthew | Weiss | MWEISS | 1987-07-07 | ST_MAN |
8000.00 | 0.00 | 100 | 50 |
| 121 | Adam | Fripp | AFRIPP | 1990-07-08 | ST_MAN |
8200.00 | 0.00 | 100 | 50 |
| 122 | Payam | Kaufling | PKAUFLIN | 1987-07-09 | ST_MAN |
7900.00 | 0.00 | 100 | 50 |
| 123 | Shanta | Vollman | SVOLLMAN | 1987-07-10 | ST_MAN |
6500.00 | 0.00 | 100 | 50 |
| 124 | Kevin | Mourgos | KMOURGOS | 1997-07-11 | ST_MAN |
5800.00 | 0.00 | 100 | 50 |
| 125 | Julia | Nayer | JNAYER | 1987-07-12 | ST_CLERK |
3200.00 | 0.00 | 120 | 50 |
| 126 | Irene | Mikkilineni | IMIKKILI | 1987-07-13 | ST_CLERK |
2700.00 | 0.00 | 120 | 50 |
| 127 | James | Landry | JLANDRY | 1978-07-14 | ST_CLERK |
2400.00 | 0.00 | 120 | 50 |
| 128 | Steven | Markle | SMARKLE | 1987-07-15 | ST_CLERK |
2200.00 | 0.00 | 120 | 50 |
| 129 | Laura | Bissot | LBISSOT | 1987-07-16 | ST_CLERK |
3300.00 | 0.00 | 121 | 50 |
| 130 | Mozhe | Atkinson | MATKINSO | 1983-07-17 | ST_CLERK |
2800.00 | 0.00 | 121 | 50 |
| 131 | James | Marlow | JAMRLOW | 1987-07-18 | ST_CLERK |
2500.00 | 0.00 | 121 | 50 |
| 132 | TJ | Olson | TJOLSON | 1987-07-19 | ST_CLERK |
2100.00 | 0.00 | 121 | 50 |
| 133 | Jason | Mallin | JMALLIN | 1985-07-20 | ST_CLERK |
3300.00 | 0.00 | 122 | 50 |
| 134 | Michael | Rogers | MROGERS | 1987-07-21 | ST_CLERK |
2900.00 | 0.00 | 122 | 50 |
| 135 | Ki | Gee | KGEE | 1987-07-22 | ST_CLERK |
2400.00 | 0.00 | 122 | 50 |
| 136 | Hazel | Philtanker | HPHILTAN | 1990-07-23 | ST_CLERK |
2200.00 | 0.00 | 122 | 50 |
| 137 | Renske | Ladwig | RLADWIG | 2000-07-24 | ST_CLERK |
3600.00 | 0.00 | 123 | 50 |
| 138 | Stephen | Stiles | SSTILES | 1987-07-25 | ST_CLERK |
3200.00 | 0.00 | 123 | 50 |
| 139 | John | Seo | JSEO | 1989-07-26 | ST_CLERK |
2700.00 | 0.00 | 123 | 50 |
| 140 | Joshua | Patel | JPATEL | 2011-07-27 | ST_CLERK |
2500.00 | 0.00 | 123 | 50 |
| 141 | Trenna | Rajs | TRAJS | 1987-07-28 | ST_CLERK |
3500.00 | 0.00 | 124 | 50 |
| 142 | Curtis | Davies | CDAVIES | 1986-07-29 | ST_CLERK |
3100.00 | 0.00 | 124 | 50 |
| 143 | Randall | Matos | RMATOS | 1987-07-30 | ST_CLERK |
2600.00 | 0.00 | 124 | 50 |
| 144 | Peter | Vargas | PVARGAS | 1998-07-31 | ST_CLERK |
2500.00 | 0.00 | 124 | 50 |
| 180 | Winston | Taylor | WTAYLOR | 1987-09-05 | SH_CLERK |
3200.00 | 0.00 | 120 | 50 |
| 181 | Jean | Fleaur | JFLEAUR | 1987-09-06 | SH_CLERK |
3100.00 | 0.00 | 120 | 50 |
| 182 | Martha | Sullivan | MSULLIVA | 1987-09-07 | SH_CLERK |
2500.00 | 0.00 | 120 | 50 |
| 183 | Girard | Geoni | GGEONI | 1987-09-08 | SH_CLERK |
2800.00 | 0.00 | 120 | 50 |
| 184 | Nandita | Sarchand | NSARCHAN | 2012-09-09 | SH_CLERK |
4200.00 | 0.00 | 121 | 50 |
| 185 | Alexis | Bull | ABULL | 1987-09-10 | SH_CLERK |
4100.00 | 0.00 | 121 | 50 |
| 186 | Julia | Dellinger | JDELLING | 1987-09-11 | SH_CLERK |
3400.00 | 0.00 | 121 | 50 |
| 187 | Anthony | Cabrio | ACABRIO | 2007-09-12 | SH_CLERK |
3000.00 | 0.00 | 121 | 50 |
| 188 | Kelly | Chung | KCHUNG | 1987-09-13 | SH_CLERK |
3800.00 | 0.00 | 122 | 50 |
| 189 | Jennifer | Dilly | JDILLY | 1987-09-14 | SH_CLERK |
3600.00 | 0.00 | 122 | 50 |
| 190 | Timothy | Gates | TGATES | 1987-09-15 | SH_CLERK |
2900.00 | 0.00 | 122 | 50 |
| 191 | Randall | Perkins | RPERKINS | 1987-09-16 | SH_CLERK |
2500.00 | 0.00 | 122 | 50 |
| 192 | Sarah | Bell | SBELL | 1987-09-17 | SH_CLERK |
4000.00 | 0.00 | 123 | 50 |
| 193 | Britney | Everett | BEVERETT | 1987-09-18 | SH_CLERK |
3900.00 | 0.00 | 123 | 50 |
| 194 | Samuel | McCain | SMCCAIN | 2000-09-19 | SH_CLERK |
3200.00 | 0.00 | 123 | 50 |
| 195 | Vance | Jones | VJONES | 1996-09-20 | SH_CLERK |
2800.00 | 0.00 | 123 | 50 |
| 196 | Alana | Walsh | AWALSH | 1987-09-21 | SH_CLERK |
3100.00 | 0.00 | 124 | 50 |
| 197 | Kevin | Feeney | KFEENEY | 1987-09-22 | SH_CLERK |
3000.00 | 0.00 | 124 | 50 |
| 198 | Donald | OConnell | DOCONNEL | 1997-09-23 | SH_CLERK |
2600.00 | 0.00 | 124 | 50 |
| 199 | Douglas | Grant | DGRANT | 1987-09-24 | SH_CLERK |
2600.00 | 0.00 | 124 | 50 |
| 103 | Alexander | Hunold | AHUNOLD | 1976-06-20 | IT_PROG |
9000.00 | 0.00 | 102 | 60 |
| 104 | Bruce | Ernst | BERNST | 1987-06-21 | IT_PROG |
6000.00 | 0.00 | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 1987-06-22 | IT_PROG |
4800.00 | 0.00 | 103 | 60 |
| 106 | Valli | Pataballa | VPATABAL | 1980-06-23 | IT_PROG |
4800.00 | 0.00 | 103 | 60 |
| 107 | Diana | Lorentz | DLORENTZ | 1987-06-24 | IT_PROG |
4200.00 | 0.00 | 103 | 60 |
| 204 | Hermann | Baer | HBAER | 1987-09-29 | PR_REP |
10000.00 | 0.00 | 101 | 70 |
| 145 | John | Russell | JRUSSEL | 1991-08-01 | SA_MAN |
14000.00 | 0.40 | 100 | 80 |
| 146 | Karen | Partners | KPARTNER | 2019-08-02 | SA_MAN |
13500.00 | 0.30 | 100 | 80 |
| 147 | Alberto | Errazuriz | AERRAZUR | 1987-08-03 | SA_MAN |
12000.00 | 0.30 | 100 | 80 |
| 148 | Gerald | Cambrault | GCAMBRAU | 2015-08-04 | SA_MAN |
11000.00 | 0.30 | 100 | 80 |
| 149 | Eleni | Zlotkey | EZLOTKEY | 1987-08-05 | SA_MAN |
10500.00 | 0.20 | 100 | 80 |
| 150 | Peter | Tucker | PTUCKER | 1987-08-06 | SA_REP |
10000.00 | 0.30 | 145 | 80 |
| 151 | David | Bernstein | DBERNSTE | 2013-08-07 | SA_REP |
9500.00 | 0.25 | 145 | 80 |
| 152 | Peter | Hall | PHALL | 1987-08-08 | SA_REP |
9000.00 | 0.25 | 145 | 80 |
| 153 | Christopher | Olsen | COLSEN | 1987-08-09 | SA_REP |
8000.00 | 0.20 | 145 | 80 |
| 154 | Nanette | Cambrault | NCAMBRAU | 1987-08-10 | SA_REP |
7500.00 | 0.20 | 145 | 80 |
| 155 | Oliver | Tuvault | OTUVAULT | 1987-08-11 | SA_REP |
7000.00 | 0.15 | 145 | 80 |
| 156 | Janette | King | JKING | 1987-08-12 | SA_REP |
10000.00 | 0.35 | 146 | 80 |
| 157 | Patrick | Sully | PSULLY | 2002-08-13 | SA_REP |
9500.00 | 0.35 | 146 | 80 |
| 158 | Allan | McEwen | AMCEWEN | 1987-08-14 | SA_REP |
9000.00 | 0.35 | 146 | 80 |
| 159 | Lindsey | Smith | LSMITH | 1987-08-15 | SA_REP |
8000.00 | 0.30 | 146 | 80 |
| 160 | Louise | Doran | LDORAN | 2003-08-16 | SA_REP |
7500.00 | 0.30 | 146 | 80 |
| 161 | Sarath | Sewall | SSEWALL | 1987-08-17 | SA_REP |
7000.00 | 0.25 | 146 | 80 |
| 162 | Clara | Vishney | CVISHNEY | 1987-08-18 | SA_REP |
10500.00 | 0.25 | 147 | 80 |
| 163 | Danielle | Greene | DGREENE | 1987-08-19 | SA_REP |
9500.00 | 0.15 | 147 | 80 |
| 164 | Mattea | Marvins | MMARVINS | 2007-08-20 | SA_REP |
7200.00 | 0.10 | 147 | 80 |
| 165 | David | Lee | DLEE | 1987-08-21 | SA_REP |
6800.00 | 0.10 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 1987-08-22 | SA_REP |
6400.00 | 0.10 | 147 | 80 |
| 167 | Amit | Banda | ABANDA | 1987-08-23 | SA_REP |
6200.00 | 0.10 | 147 | 80 |
| 168 | Lisa | Ozer | LOZER | 1987-08-24 | SA_REP |
11500.00 | 0.25 | 148 | 80 |
| 169 | Harrison | Bloom | HBLOOM | 1999-08-25 | SA_REP |
10000.00 | 0.20 | 148 | 80 |
| 170 | Tayler | Fox | TFOX | 1987-08-26 | SA_REP |
9600.00 | 0.20 | 148 | 80 |
| 171 | William | Smith | WSMITH | 1987-08-27 | SA_REP |
7400.00 | 0.15 | 148 | 80 |
| 172 | Elizabeth | Bates | EBATES | 1987-08-28 | SA_REP |
7300.00 | 0.15 | 148 | 80 |
| 173 | Sundita | Kumar | SKUMAR | 1987-08-29 | SA_REP |
6100.00 | 0.10 | 148 | 80 |
| 174 | Ellen | Abel | EABEL | 1987-08-30 | SA_REP |
11000.00 | 0.30 | 149 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 1996-08-31 | SA_REP |
8800.00 | 0.25 | 149 | 80 |
| 176 | Jonathon | Taylor | JTAYLOR | 1987-09-01 | SA_REP |
8600.00 | 0.20 | 149 | 80 |
| 177 | Jack | Livingston | JLIVINGS | 1987-09-02 | SA_REP |
8400.00 | 0.20 | 149 | 80 |
| 179 | Charles | Johnson | CJOHNSON | 1999-09-04 | SA_REP |
6200.00 | 0.10 | 149 | 80 |
| 100 | Steven | King | SKING | 1998-06-17 | AD_PRES |
24000.00 | 0.00 | 0 | 90 |
| 101 | Neena | Kochhar | NKOCHHAR | 1987-06-18 | AD_VP |
17000.00 | 0.00 | 100 | 90 |
| 102 | Lex | De Haan | LDEHAAN | 1987-06-19 | AD_VP |
17000.00 | 0.00 | 100 | 90 |
| 108 | Nancy | Greenberg | NGREENBE | 1987-06-25 | FI_MGR |
12000.00 | 0.00 | 101 | 100 |
| 109 | Daniel | Faviet | DFAVIET | 1992-06-26 | FI_ACCOUNT |
9000.00 | 0.00 | 108 | 100 |
| 110 | John | Chen | JCHEN | 1987-06-27 | FI_ACCOUNT |
8200.00 | 0.00 | 108 | 100 |
| 111 | Ismael | Sciarra | ISCIARRA | 1987-06-28 | FI_ACCOUNT |
7700.00 | 0.00 | 108 | 100 |
| 112 | Jose Manuel | Urman | JMURMAN | 1981-06-29 | FI_ACCOUNT |
7800.00 | 0.00 | 108 | 100 |
| 113 | Luis | Popp | LPOPP | 1987-06-30 | FI_ACCOUNT |
6900.00 | 0.00 | 108 | 100 |
| 205 | Shelley | Higgins | SHIGGINS | 1987-09-30 | AC_MGR |
12000.00 | 0.00 | 101 | 110 |
| 206 | William | Gietz | WGIETZ | 1997-10-01 | AC_ACCOUNT |
8300.00 | 0.00 | 205 | 110 |
+--------+-------------+-------------+----------+------------+------------
+----------+----------------+--------+--------+

mysql> select * from myemp order by dep_id asc;

mysql> select * from myemp order by dep_id desc;

mysql> select * from myemp order by first_name asc;

mysql> select * from myemp order by first_name asc, salary asc;


+--------+-------------+-------------+----------+------------+------------
+----------+----------------+--------+--------+
| EMP_ID | FIRST_NAME | LAST_NAME | EMAIL | HIRE_DATE | JOB_ID | SALARY
| COMMISSION_PCT | MGR_ID | DEP_ID |
+--------+-------------+-------------+----------+------------+------------
+----------+----------------+--------+--------+
| 121 | Adam | Fripp | AFRIPP | 1990-07-08 | ST_MAN |
8200.00 | 0.00 | 100 | 50 |
| 196 | Alana | Walsh | AWALSH | 1987-09-21 | SH_CLERK |
3100.00 | 0.00 | 124 | 50 |
| 147 | Alberto | Errazuriz | AERRAZUR | 1987-08-03 | SA_MAN |
12000.00 | 0.30 | 100 | 80 |
| 115 | Alexander | Khoo | AKHOO | 1983-07-02 | PU_CLERK |
3100.00 | 0.00 | 114 | 30 |
| 103 | Alexander | Hunold | AHUNOLD | 1976-06-20 | IT_PROG |
9000.00 | 0.00 | 102 | 60 |
| 185 | Alexis | Bull | ABULL | 1987-09-10 | SH_CLERK |
4100.00 | 0.00 | 121 | 50 |
| 158 | Allan | McEwen | AMCEWEN | 1987-08-14 | SA_REP |
9000.00 | 0.35 | 146 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 1996-08-31 | SA_REP |
8800.00 | 0.25 | 149 | 80 |
| 167 | Amit | Banda | ABANDA | 1987-08-23 | SA_REP |
6200.00 | 0.10 | 147 | 80 |
| 187 | Anthony | Cabrio | ACABRIO | 2007-09-12 | SH_CLERK |
3000.00 | 0.00 | 121 | 50 |
| 193 | Britney | Everett | BEVERETT | 1987-09-18 | SH_CLERK |
3900.00 | 0.00 | 123 | 50 |
| 104 | Bruce | Ernst | BERNST | 1987-06-21 | IT_PROG |
6000.00 | 0.00 | 103 | 60 |
| 179 | Charles | Johnson | CJOHNSON | 1999-09-04 | SA_REP |
6200.00 | 0.10 | 149 | 80 |
| 153 | Christopher | Olsen | COLSEN | 1987-08-09 | SA_REP |
8000.00 | 0.20 | 145 | 80 |
| 162 | Clara | Vishney | CVISHNEY | 1987-08-18 | SA_REP |
10500.00 | 0.25 | 147 | 80 |
| 142 | Curtis | Davies | CDAVIES | 1986-07-29 | ST_CLERK |
3100.00 | 0.00 | 124 | 50 |
| 109 | Daniel | Faviet | DFAVIET | 1992-06-26 | FI_ACCOUNT |
9000.00 | 0.00 | 108 | 100 |
| 163 | Danielle | Greene | DGREENE | 1987-08-19 | SA_REP |
9500.00 | 0.15 | 147 | 80 |
| 105 | David | Austin | DAUSTIN | 1987-06-22 | IT_PROG |
4800.00 | 0.00 | 103 | 60 |
| 165 | David | Lee | DLEE | 1987-08-21 | SA_REP |
6800.00 | 0.10 | 147 | 80 |
| 151 | David | Bernstein | DBERNSTE | 2013-08-07 | SA_REP |
9500.00 | 0.25 | 145 | 80 |
| 114 | Den | Raphaely | DRAPHEAL | 1987-07-01 | PU_MAN |
11000.00 | 0.00 | 100 | 30 |
| 107 | Diana | Lorentz | DLORENTZ | 1987-06-24 | IT_PROG |
4200.00 | 0.00 | 103 | 60 |
| 198 | Donald | OConnell | DOCONNEL | 1997-09-23 | SH_CLERK |
2600.00 | 0.00 | 124 | 50 |
| 199 | Douglas | Grant | DGRANT | 1987-09-24 | SH_CLERK |
2600.00 | 0.00 | 124 | 50 |
| 149 | Eleni | Zlotkey | EZLOTKEY | 1987-08-05 | SA_MAN |
10500.00 | 0.20 | 100 | 80 |
| 172 | Elizabeth | Bates | EBATES | 1987-08-28 | SA_REP |
7300.00 | 0.15 | 148 | 80 |
| 174 | Ellen | Abel | EABEL | 1987-08-30 | SA_REP |
11000.00 | 0.30 | 149 | 80 |
| 148 | Gerald | Cambrault | GCAMBRAU | 2015-08-04 | SA_MAN |
11000.00 | 0.30 | 100 | 80 |
| 183 | Girard | Geoni | GGEONI | 1987-09-08 | SH_CLERK |
2800.00 | 0.00 | 120 | 50 |
| 118 | Guy | Himuro | GHIMURO | 1995-07-05 | PU_CLERK |
2600.00 | 0.00 | 114 | 30 |
| 169 | Harrison | Bloom | HBLOOM | 1999-08-25 | SA_REP |
10000.00 | 0.20 | 148 | 80 |
| 136 | Hazel | Philtanker | HPHILTAN | 1990-07-23 | ST_CLERK |
2200.00 | 0.00 | 122 | 50 |
| 204 | Hermann | Baer | HBAER | 1987-09-29 | PR_REP |
10000.00 | 0.00 | 101 | 70 |
| 126 | Irene | Mikkilineni | IMIKKILI | 1987-07-13 | ST_CLERK |
2700.00 | 0.00 | 120 | 50 |
| 111 | Ismael | Sciarra | ISCIARRA | 1987-06-28 | FI_ACCOUNT |
7700.00 | 0.00 | 108 | 100 |
| 177 | Jack | Livingston | JLIVINGS | 1987-09-02 | SA_REP |
8400.00 | 0.20 | 149 | 80 |
| 127 | James | Landry | JLANDRY | 1978-07-14 | ST_CLERK |
2400.00 | 0.00 | 120 | 50 |
| 131 | James | Marlow | JAMRLOW | 1987-07-18 | ST_CLERK |
2500.00 | 0.00 | 121 | 50 |
| 156 | Janette | King | JKING | 1987-08-12 | SA_REP |
10000.00 | 0.35 | 146 | 80 |
| 133 | Jason | Mallin | JMALLIN | 1985-07-20 | ST_CLERK |
3300.00 | 0.00 | 122 | 50 |
| 181 | Jean | Fleaur | JFLEAUR | 1987-09-06 | SH_CLERK |
3100.00 | 0.00 | 120 | 50 |
| 189 | Jennifer | Dilly | JDILLY | 1987-09-14 | SH_CLERK |
3600.00 | 0.00 | 122 | 50 |
| 200 | Jennifer | Whalen | JWHALEN | 1988-09-25 | AD_ASST |
4400.00 | 0.00 | 101 | 10 |
| 139 | John | Seo | JSEO | 1989-07-26 | ST_CLERK |
2700.00 | 0.00 | 123 | 50 |
| 110 | John | Chen | JCHEN | 1987-06-27 | FI_ACCOUNT |
8200.00 | 0.00 | 108 | 100 |
| 145 | John | Russell | JRUSSEL | 1991-08-01 | SA_MAN |
14000.00 | 0.40 | 100 | 80 |
| 176 | Jonathon | Taylor | JTAYLOR | 1987-09-01 | SA_REP |
8600.00 | 0.20 | 149 | 80 |
| 112 | Jose Manuel | Urman | JMURMAN | 1981-06-29 | FI_ACCOUNT |
7800.00 | 0.00 | 108 | 100 |
| 140 | Joshua | Patel | JPATEL | 2011-07-27 | ST_CLERK |
2500.00 | 0.00 | 123 | 50 |
| 125 | Julia | Nayer | JNAYER | 1987-07-12 | ST_CLERK |
3200.00 | 0.00 | 120 | 50 |
| 186 | Julia | Dellinger | JDELLING | 1987-09-11 | SH_CLERK |
3400.00 | 0.00 | 121 | 50 |
| 119 | Karen | Colmenares | KCOLMENA | 1987-07-06 | PU_CLERK |
2500.00 | 0.00 | 114 | 30 |
| 146 | Karen | Partners | KPARTNER | 2019-08-02 | SA_MAN |
13500.00 | 0.30 | 100 | 80 |
| 188 | Kelly | Chung | KCHUNG | 1987-09-13 | SH_CLERK |
3800.00 | 0.00 | 122 | 50 |
| 197 | Kevin | Feeney | KFEENEY | 1987-09-22 | SH_CLERK |
3000.00 | 0.00 | 124 | 50 |
| 124 | Kevin | Mourgos | KMOURGOS | 1997-07-11 | ST_MAN |
5800.00 | 0.00 | 100 | 50 |
| 135 | Ki | Gee | KGEE | 1987-07-22 | ST_CLERK |
2400.00 | 0.00 | 122 | 50 |
| 129 | Laura | Bissot | LBISSOT | 1987-07-16 | ST_CLERK |
3300.00 | 0.00 | 121 | 50 |
| 102 | Lex | De Haan | LDEHAAN | 1987-06-19 | AD_VP |
17000.00 | 0.00 | 100 | 90 |
| 159 | Lindsey | Smith | LSMITH | 1987-08-15 | SA_REP |
8000.00 | 0.30 | 146 | 80 |
| 168 | Lisa | Ozer | LOZER | 1987-08-24 | SA_REP |
11500.00 | 0.25 | 148 | 80 |
| 160 | Louise | Doran | LDORAN | 2003-08-16 | SA_REP |
7500.00 | 0.30 | 146 | 80 |
| 113 | Luis | Popp | LPOPP | 1987-06-30 | FI_ACCOUNT |
6900.00 | 0.00 | 108 | 100 |
| 182 | Martha | Sullivan | MSULLIVA | 1987-09-07 | SH_CLERK |
2500.00 | 0.00 | 120 | 50 |
| 164 | Mattea | Marvins | MMARVINS | 2007-08-20 | SA_REP |
7200.00 | 0.10 | 147 | 80 |
| 120 | Matthew | Weiss | MWEISS | 1987-07-07 | ST_MAN |
8000.00 | 0.00 | 100 | 50 |
| 134 | Michael | Rogers | MROGERS | 1987-07-21 | ST_CLERK |
2900.00 | 0.00 | 122 | 50 |
| 201 | Michael | Hartstein | MHARTSTE | 1987-09-26 | MK_MAN |
13000.00 | 0.00 | 100 | 20 |
| 130 | Mozhe | Atkinson | MATKINSO | 1983-07-17 | ST_CLERK |
2800.00 | 0.00 | 121 | 50 |
| 108 | Nancy | Greenberg | NGREENBE | 1987-06-25 | FI_MGR |
12000.00 | 0.00 | 101 | 100 |
| 184 | Nandita | Sarchand | NSARCHAN | 2012-09-09 | SH_CLERK |
4200.00 | 0.00 | 121 | 50 |
| 154 | Nanette | Cambrault | NCAMBRAU | 1987-08-10 | SA_REP |
7500.00 | 0.20 | 145 | 80 |
| 101 | Neena | Kochhar | NKOCHHAR | 1987-06-18 | AD_VP |
17000.00 | 0.00 | 100 | 90 |
| 155 | Oliver | Tuvault | OTUVAULT | 1987-08-11 | SA_REP |
7000.00 | 0.15 | 145 | 80 |
| 202 | Pat | Fay | PFAY | 1987-09-27 | MK_REP |
6000.00 | 0.00 | 201 | 20 |
| 157 | Patrick | Sully | PSULLY | 2002-08-13 | SA_REP |
9500.00 | 0.35 | 146 | 80 |
| 122 | Payam | Kaufling | PKAUFLIN | 1987-07-09 | ST_MAN |
7900.00 | 0.00 | 100 | 50 |
| 144 | Peter | Vargas | PVARGAS | 1998-07-31 | ST_CLERK |
2500.00 | 0.00 | 124 | 50 |
| 152 | Peter | Hall | PHALL | 1987-08-08 | SA_REP |
9000.00 | 0.25 | 145 | 80 |
| 150 | Peter | Tucker | PTUCKER | 1987-08-06 | SA_REP |
10000.00 | 0.30 | 145 | 80 |
| 191 | Randall | Perkins | RPERKINS | 1987-09-16 | SH_CLERK |
2500.00 | 0.00 | 122 | 50 |
| 143 | Randall | Matos | RMATOS | 1987-07-30 | ST_CLERK |
2600.00 | 0.00 | 124 | 50 |
| 137 | Renske | Ladwig | RLADWIG | 2000-07-24 | ST_CLERK |
3600.00 | 0.00 | 123 | 50 |
| 194 | Samuel | McCain | SMCCAIN | 2000-09-19 | SH_CLERK |
3200.00 | 0.00 | 123 | 50 |
| 192 | Sarah | Bell | SBELL | 1987-09-17 | SH_CLERK |
4000.00 | 0.00 | 123 | 50 |
| 161 | Sarath | Sewall | SSEWALL | 1987-08-17 | SA_REP |
7000.00 | 0.25 | 146 | 80 |
| 123 | Shanta | Vollman | SVOLLMAN | 1987-07-10 | ST_MAN |
6500.00 | 0.00 | 100 | 50 |
| 205 | Shelley | Higgins | SHIGGINS | 1987-09-30 | AC_MGR |
12000.00 | 0.00 | 101 | 110 |
| 116 | Shelli | Baida | SBAIDA | 1987-07-03 | PU_CLERK |
2900.00 | 0.00 | 114 | 30 |
| 117 | Sigal | Tobias | STOBIAS | 1987-07-04 | PU_CLERK |
2800.00 | 0.00 | 114 | 30 |
| 138 | Stephen | Stiles | SSTILES | 1987-07-25 | ST_CLERK |
3200.00 | 0.00 | 123 | 50 |
| 128 | Steven | Markle | SMARKLE | 1987-07-15 | ST_CLERK |
2200.00 | 0.00 | 120 | 50 |
| 100 | Steven | King | SKING | 1998-06-17 | AD_PRES |
24000.00 | 0.00 | 0 | 90 |
| 166 | Sundar | Ande | SANDE | 1987-08-22 | SA_REP |
6400.00 | 0.10 | 147 | 80 |
| 173 | Sundita | Kumar | SKUMAR | 1987-08-29 | SA_REP |
6100.00 | 0.10 | 148 | 80 |
| 203 | Susan | Mavris | SMAVRIS | 1989-09-28 | HR_REP |
6500.00 | 0.00 | 101 | 40 |
| 170 | Tayler | Fox | TFOX | 1987-08-26 | SA_REP |
9600.00 | 0.20 | 148 | 80 |
| 190 | Timothy | Gates | TGATES | 1987-09-15 | SH_CLERK |
2900.00 | 0.00 | 122 | 50 |
| 132 | TJ | Olson | TJOLSON | 1987-07-19 | ST_CLERK |
2100.00 | 0.00 | 121 | 50 |
| 141 | Trenna | Rajs | TRAJS | 1987-07-28 | ST_CLERK |
3500.00 | 0.00 | 124 | 50 |
| 106 | Valli | Pataballa | VPATABAL | 1980-06-23 | IT_PROG |
4800.00 | 0.00 | 103 | 60 |
| 195 | Vance | Jones | VJONES | 1996-09-20 | SH_CLERK |
2800.00 | 0.00 | 123 | 50 |
| 171 | William | Smith | WSMITH | 1987-08-27 | SA_REP |
7400.00 | 0.15 | 148 | 80 |
| 206 | William | Gietz | WGIETZ | 1997-10-01 | AC_ACCOUNT |
8300.00 | 0.00 | 205 | 110 |
| 180 | Winston | Taylor | WTAYLOR | 1987-09-05 | SH_CLERK |
3200.00 | 0.00 | 120 | 50 |
+--------+-------------+-------------+----------+------------+------------
+----------+----------------+--------+--------+

grouping record
----------------

select <colName>
from <tableName>
group by <colname>;

functions
----------
row level function -------> on every row, 1 o/p per row.

mysql> select emp_id, first_name, hire_date, year(hire_date) as year, salary,


dep_id from myemp;

group | aggregate function


--------------------------
-> applicable only for cols

sum()
avg()
min()
max()
count() etc...

mysql> select sum(salary), avg(salary), max(salary), min(salary), count(salary)


from myemp;
+-------------+-------------+-------------+-------------+---------------+
| sum(salary) | avg(salary) | max(salary) | min(salary) | count(salary) |
+-------------+-------------+-------------+-------------+---------------+
| 684400.00 | 6456.603774 | 24000.00 | 2100.00 | 106 |
+-------------+-------------+-------------+-------------+---------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> select sum(salary) as sumsalary, avg(salary) as avgsalary, max(salary) as
maxsalary, min(salary) as minsalary, count(salary) as contsalary from myemp;
+-----------+-------------+-----------+-----------+------------+
| sumsalary | avgsalary | maxsalary | minsalary | contsalary |
+-----------+-------------+-----------+-----------+------------+
| 684400.00 | 6456.603774 | 24000.00 | 2100.00 | 106 |
+-----------+-------------+-----------+-----------+------------+

mysql> select dep_id, sum(salary) as totalsalary from myemp group by dep_id;


+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 90 | 58000.00 |
| 60 | 28800.00 |
| 100 | 51600.00 |
| 30 | 24900.00 |
| 50 | 156400.00 |
| 80 | 304500.00 |
| 10 | 4400.00 |
| 20 | 19000.00 |
| 40 | 6500.00 |
| 70 | 10000.00 |
| 110 | 20300.00 |
+--------+-------------+
11 rows in set (0.01 sec)

mysql> select dep_id, sum(salary) as totalsalary from myemp group by dep_id order
by dep_id;
+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 10 | 4400.00 |
| 20 | 19000.00 |
| 30 | 24900.00 |
| 40 | 6500.00 |
| 50 | 156400.00 |
| 60 | 28800.00 |
| 70 | 10000.00 |
| 80 | 304500.00 |
| 90 | 58000.00 |
| 100 | 51600.00 |
| 110 | 20300.00 |
+--------+-------------+
11 rows in set (0.00 sec)

mysql> select dep_id, sum(salary) as totalsalary from myemp group by dep_id having
dep_id=100;
+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 100 | 51600.00 |
+--------+-------------+
1 row in set (0.01 sec)

mysql> select dep_id, sum(salary) as totalsalary from myemp group by dep_id having
dep_id=20;
+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 20 | 19000.00 |
+--------+-------------+
1 row in set (0.00 sec)

mysql> select dep_id, sum(salary) as totalsalary from myemp where dep_id in


(20,40,60,100) group by dep_id;
+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 60 | 28800.00 |
| 100 | 51600.00 |
| 20 | 19000.00 |
| 40 | 6500.00 |
+--------+-------------+
4 rows in set (0.01 sec)

mysql> select dep_id, sum(salary) as totalsalary from myemp group by dep_id having
dep_id in (20,40,60,100);
+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 60 | 28800.00 |
| 100 | 51600.00 |
| 20 | 19000.00 |
| 40 | 6500.00 |
+--------+-------------+
4 rows in set (0.00 sec)

mysql> select dep_id, sum(salary) as totalsalary from myemp group by dep_id having
dep_id in (20,40,60,100) order by dep_id desc;
+--------+-------------+
| dep_id | totalsalary |
+--------+-------------+
| 100 | 51600.00 |
| 60 | 28800.00 |
| 40 | 6500.00 |
| 20 | 19000.00 |
+--------+-------------+
4 rows in set (0.00 sec)
between opertor
---------------
mysql> select * from myemp where salary between 10000 and 50000;

DCL TCL SET JOINS SUBQUERIES Constrainst


------------------------------------------------

SET Operators
--------------

Set operators are used to combine result of two or more SQL queries into a single
result.

When we are working with SET operators then make sure your sql queries must return
same number
of column and compatible datatypes.

emp >>> empno(number), ename varchar() job varchar

SQL>>> select empno, ename, job from emp;

dept>>> deptno(number) dname varchar() job varchar()

SQL>>> select deptno, dname, job from dept.

mysql supports 3 types of set operators


----------------------------------------

1) union (or) union all

2) intersect

3) minus (or) except

1) union (or) union all


--------------------
Union combines the result of two result sets and remove duplicates.

union all doesn't remove duplicates rows.

mysql> create table countries(country_id int primary key, country_name varchar(2


0), code varchar(7));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into countries values(1, 'INDIA', 'IND');


Query OK, 1 row affected (0.00 sec)

mysql> insert into countries values(2, 'united states', 'US');


Query OK, 1 row affected (0.00 sec)
mysql> create table countries_1(country_id int primary key, country_name varchar
(20), code varchar(7));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into countries_1 values(1, 'INDIA', 'IND');


Query OK, 1 row affected (0.01 sec)

mysql> insert into countries_1 values(2, 'united states', 'US');


Query OK, 1 row affected (0.01 sec)

mysql>
mysql> insert into countries_1 values(3, 'Ireland', 'IRE');
Query OK, 1 row affected (0.00 sec)

mysql> insert into countries_1 values(4, 'DUBAI', 'UAE');


Query OK, 1 row affected (0.00 sec)

mysql> insert into countries_1 values(5, 'JAPAN', 'JAP');


Query OK, 1 row affected (0.00 sec)

mysql> select * from countries UNION SELECT * from COUNTRIES_1;


+------------+---------------+------+
| country_id | country_name | code |
+------------+---------------+------+
| 1 | INDIA | IND |
| 2 | united states | US |
| 3 | Ireland | IRE |
| 4 | DUBAI | UAE |
| 5 | JAPAN | JAP |
+------------+---------------+------+
5 rows in set (0.00 sec)

mysql> select * from countries UNION all SELECT * from COUNTRIES_1;


+------------+---------------+------+
| country_id | country_name | code |
+------------+---------------+------+
| 1 | INDIA | IND |
| 2 | united states | US |
| 1 | INDIA | IND |
| 2 | united states | US |
| 3 | Ireland | IRE |
| 4 | DUBAI | UAE |
| 5 | JAPAN | JAP |
+------------+---------------+------+
7 rows in set (0.00 sec)

intersect
---------
Intersect returns only rows that appear in both result sets.

mysql> select * from countries intersect SELECT * from COUNTRIES_1;


+------------+---------------+------+
| country_id | country_name | code |
+------------+---------------+------+
| 1 | INDIA | IND |
| 2 | united states | US |
+------------+---------------+------+
2 rows in set (0.00 sec)

minus
------

mysql> select * from countries_1 except SELECT * from COUNTRIES;


+------------+--------------+------+
| country_id | country_name | code |
+------------+--------------+------+
| 3 | Ireland | IRE |
| 4 | DUBAI | UAE |
| 5 | JAPAN | JAP |
+------------+--------------+------+
3 rows in set (0.00 sec)

Joining tables (joins)


----------------------

If we have the data in different tables, then we can join those 2 tables
based on some
condition, we will get a new table.
we can fetch the data from both tables by using single select statement.

1) inner join
2) left join (left outer join)
3) right join (right outer join)
4) cross join
5) self join

Note:- both the tables must have atleast one common col.

1) inner join
-----------

mysql> select * from movies inner join members on movieid=id;


+------+--------------------------+------------+-------+------------+-----------
+---------+
| id | title | category | memid | first_name | last_name |
movieid |
+------+--------------------------+------------+-------+------------+-----------
+---------+
| 1 | ASSASSIN'S CREED: EMBERS | Animations | 1 | Alicia | Alarcon |
1 |
| 2 | Real Steel | Animations | 2 | Don | Draper |
2 |
| 5 | Safe | Action | 3 | Lizzie | Moss |
5 |
| 8 | Deadline 2009 | 18+ | 4 | Eldon | Chance |
8 |
| 10 | Marley and me | Romance | 5 | Jenny | Patterson |
10 |
+------+--------------------------+------------+-------+------------+-----------
+---------+
5 rows in set (0.00 sec)

mysql> select * from movies inner join members on movieid=id where


category='Animations';
+------+--------------------------+------------+-------+------------+-----------
+---------+
| id | title | category | memid | first_name | last_name |
movieid |
+------+--------------------------+------------+-------+------------+-----------
+---------+
| 1 | ASSASSIN'S CREED: EMBERS | Animations | 1 | Alicia | Alarcon |
1 |
| 2 | Real Steel | Animations | 2 | Don | Draper |
2 |
+------+--------------------------+------------+-------+------------+-----------
+---------+

LEFT JOIN
----------

mysql> select * from movies left join members on movieid=id;


+------+---------------------------+------------+-------+------------+-----------
+---------+
| id | title | category | memid | first_name | last_name |
movieid |
+------+---------------------------+------------+-------+------------+-----------
+---------+
| 1 | ASSASSIN'S CREED: EMBERS | Animations | 1 | Alicia | Alarcon |
1 |
| 2 | Real Steel | Animations | 2 | Don | Draper |
2 |
| 3 | Alvin and the Chipmunks | Animations | NULL | NULL | NULL |
NULL |
| 4 | The Adventures of Tin Tin | Animations | NULL | NULL | NULL |
NULL |
| 5 | Safe | Action | 3 | Lizzie | Moss |
5 |
| 6 | Safe House | Action | NULL | NULL | NULL |
NULL |
| 7 | GIA | 18+ | NULL | NULL | NULL |
NULL |
| 8 | Deadline 2009 | 18+ | 4 | Eldon | Chance |
8 |
| 9 | The Dirty Picture | 18+ | NULL | NULL | NULL |
NULL |
| 10 | Marley and me | Romance | 5 | Jenny | Patterson |
10 |
+------+---------------------------+------------+-------+------------+-----------
+---------+
10 rows in set (0.00 sec)
Right Join
-----------

mysql> select * from movies right join members on movieid=id;


+------+--------------------------+------------+-------+------------+-----------
+---------+
| id | title | category | memid | first_name | last_name |
movieid |
+------+--------------------------+------------+-------+------------+-----------
+---------+
| 1 | ASSASSIN'S CREED: EMBERS | Animations | 1 | Alicia | Alarcon |
1 |
| 2 | Real Steel | Animations | 2 | Don | Draper |
2 |
| 5 | Safe | Action | 3 | Lizzie | Moss |
5 |
| 8 | Deadline 2009 | 18+ | 4 | Eldon | Chance |
8 |
| 10 | Marley and me | Romance | 5 | Jenny | Patterson |
10 |
| NULL | NULL | NULL | 6 | Craig | Daniels |
NULL |
| NULL | NULL | NULL | 7 | Denny | Peters |
NULL |
| NULL | NULL | NULL | 8 | Patty | Pattinson |
NULL |
+------+--------------------------+------------+-------+------------+-----------
+---------+
8 rows in set (0.00 sec)

cross join
----------
mysql> select * from movies cross join members;

mysql> select * from meals cross join drinks;


+----------+-------+-----------+-------+
| mealname | rate | drinkname | rate |
+----------+-------+-----------+-------+
| Pancake | 40.75 | Tea | 5.00 |
| Sausage | 15.50 | Tea | 5.00 |
| Omlet | 10.50 | Tea | 5.00 |
| Pancake | 40.75 | Coffee | 15.00 |
| Sausage | 15.50 | Coffee | 15.00 |
| Omlet | 10.50 | Coffee | 15.00 |
| Pancake | 40.75 | Pepsi | 20.00 |
| Sausage | 15.50 | Pepsi | 20.00 |
| Omlet | 10.50 | Pepsi | 20.00 |
+----------+-------+-----------+-------+
9 rows in set (0.00 sec)

self join
---------

You might also like