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

Mysql Record File

The document describes various SQL commands used to manage databases and tables in MySQL. It shows how to create and drop databases, create and populate a table, insert, select, update and delete data. Key SQL commands covered include CREATE DATABASE, DROP DATABASE, CREATE TABLE, INSERT, SELECT, DELETE, SHOW TABLES, DESCRIBE. Examples demonstrate how to select specific columns, filter rows, perform calculations and use comparison operators in queries.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Mysql Record File

The document describes various SQL commands used to manage databases and tables in MySQL. It shows how to create and drop databases, create and populate a table, insert, select, update and delete data. Key SQL commands covered include CREATE DATABASE, DROP DATABASE, CREATE TABLE, INSERT, SELECT, DELETE, SHOW TABLES, DESCRIBE. Examples demonstrate how to select specific columns, filter rows, perform calculations and use comparison operators in queries.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

MYSql Queries

List of the databases we have:

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| carbrands |

| information_schema |

| mysql |

| performance_schema |

| school |

| sys |

+--------------------+

6 rows in set (0.02 sec)

Create a new database using SQL command "CREATE DATABASE databaseName"; and delete a

database using "DROP DATABASE databaseName".

mysql> create database automobiles;

Query OK, 1 row affected (0.01 sec)

mysql> drop database automobiles;

Query OK, 0 rows affected (0.01 sec)

USE database command activates the database which we need to use:

mysql> use automobiles;

Database changed

Select database command used to display the current default database:

mysql> select database();

+-------------+

| database() |

+-------------+

| automobiles |

+-------------+

1 row in set (0.00 sec)


Showing all the databases present in mysql server:

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| automobiles |

| carbrands |

| information_schema |

| mysql |

| performance_schema |

| school |

| sys |

+--------------------+

7 rows in set (0.00 sec)

Showing all the tables in the current database ‘automobiles’:

mysql> show tables;

Empty set (0.02 sec)

Creating a table in the database:

mysql> create table customer(custid char(5) primary key, car_name varchar(25), engine_id
varchar(10), purchase_year int);

Query OK, 0 rows affected (0.07 sec)

Checking whether the table has been created:

mysql> show tables;

+-----------------------+

| Tables_in_automobiles |

+-----------------------+

| customer |

+-----------------------+

1 row in set (0.01 sec)

Describing the fields of the database:

mysql> describe customer;


+---------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------------+-------------+------+-----+---------+-------+

| custid | char(5) | NO | PRI | NULL | |

| car_name | varchar(25) | YES | | NULL | |

| engine_id | varchar(10) | YES | | NULL | |

| purchase_year | int | YES | | NULL | |

+---------------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

Inserting a value into the table ‘Customer’

mysql> insert into customer values('C01', 'Honda City', '25XUC20394', 2018);

Query OK, 1 row affected (0.01 sec)

Inserting multiple values into the database:

mysql> insert into customer values('C02', 'Mahindra Scorpio-N', '75POY61945', 2022), ('C03', 'Toyota
Fortuner', '86TOZ71464', 2021), ('C04', 'Mahindra Thar', '70QRA82590', 2021), ('C05', 'Hyundai i20',
'30BXP62481', 2018), ('C06', 'Toyota Yaris', '51XCF52945', 2019), ('C07', 'Honda Jazz', '26PYI62012',
2020), ('C08', 'Mahindra XUV700', '70QPC96101', 2022), ('C09', 'Hyundai Creta', '45CXA75019',
2022), ('C10', 'Toyota Innova Crysta', '82TOX81965', 2022);

Query OK, 9 rows affected (0.01 sec)

Records: 9 Duplicates: 0 Warnings: 0

Inserting value to selected columns:

mysql> insert into customer(custid, car_name) values('C11', 'KIA Carens');

Query OK, 1 row affected (0.01 sec)

Selecting all the records from the database:

mysql> select * from customer;

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C01 | Honda City | 25XUC20394 | 2018 |

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |


| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C11 | KIA Carens | NULL | NULL |

+--------+----------------------+------------+---------------+

11 rows in set (0.00 sec)

Deleting a row from the database:

mysql> delete from customer where custid='C01';

Query OK, 1 row affected (0.01 sec)

Selecting records for specific columns from the database:

mysql> select custid, car_name from customer;

+--------+----------------------+

| custid | car_name |

+--------+----------------------+

| C02 | Mahindra Scorpio-N |

| C03 | Toyota Fortuner |

| C04 | Mahindra Thar |

| C05 | Hyundai i20 |

| C06 | Toyota Yaris |

| C07 | Honda Jazz |

| C08 | Mahindra XUV700 |

| C09 | Hyundai Creta |

| C10 | Toyota Innova Crysta |

| C11 | KIA Carens |

+--------+----------------------+

10 rows in set (0.00 sec)

Listing all records:

mysql> select * from customer;


+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C11 | KIA Carens | NULL | NULL |

+--------+----------------------+------------+---------------+

10 rows in set (0.00 sec)

Adding two numbers:

mysql> select 100+400;

+---------+

| 100+400 |

+---------+

| 500 |

+---------+

1 row in set (0.00 sec)

Selecting the current time:

mysql> select now();

+---------------------+

| now() |

+---------------------+

| 2022-10-19 18:04:36 |

+---------------------+

1 row in set (0.00 sec)


Selecting the addition of 2 numbers as well as the current time:

mysql> select 100+400, power(2,3);

+---------+------------+

| 100+400 | power(2,3) |

+---------+------------+

| 500 | 8|

+---------+------------+

1 row in set (0.01 sec)

Comparison operators:

Selecting the car name and customer id where year of purchase is greater than 2018:

mysql> select car_name, custid from customer where purchase_year>2018;

+----------------------+--------+

| car_name | custid |

+----------------------+--------+

| Mahindra Scorpio-N | C02 |

| Toyota Fortuner | C03 |

| Mahindra Thar | C04 |

| Toyota Yaris | C06 |

| Honda Jazz | C07 |

| Mahindra XUV700 | C08 |

| Hyundai Creta | C09 |

| Toyota Innova Crysta | C10 |

+----------------------+--------+

8 rows in set (0.00 sec)

Selecting the car name and customer id for KIA Carens:

mysql> select car_name, custid from customer where car_name='Kia Carens';

+------------+--------+

| car_name | custid |

+------------+--------+

| KIA Carens | C11 |

+------------+--------+
1 row in set (0.00 sec)

String Pattern Matching(like or not like):

mysql> select * from customer where car_name like 'Mahindra%';

+--------+--------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+--------------------+------------+---------------+

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

+--------+--------------------+------------+---------------+

3 rows in set (0.00 sec)

mysql> select * from customer where car_name like 'H__%';

+--------+---------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+---------------+------------+---------------+

| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

+--------+---------------+------------+---------------+

3 rows in set (0.00 sec)

mysql> select * from customer where car_name like 'H__%' and purchase_year>2020;

+--------+---------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+---------------+------------+---------------+

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

+--------+---------------+------------+---------------+

1 row in set (0.00 sec)

mysql> select * from customer where not(car_name like 'H__%' and purchase_year>2020);

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+
| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C11 | KIA Carens | NULL | NULL |

+--------+----------------------+------------+---------------+

9 rows in set (0.00 sec)

BETWEEN, NOT BETWEEN:

mysql> select * from customer where purchase_year between 2019 and 2021;

+--------+-----------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+-----------------+------------+---------------+

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

+--------+-----------------+------------+---------------+

4 rows in set (0.00 sec)

Null/not null commands:

mysql> select * from customer where engine_id is null;

+--------+------------+-----------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+------------+-----------+---------------+

| C11 | KIA Carens | NULL | NULL |

+--------+------------+-----------+---------------+

1 row in set (0.00 sec)


mysql> select * from customer where car_name is null;

Empty set (0.00 sec)

ORDER BY Commands:

mysql> select * from customer order by car_name;

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C11 | KIA Carens | NULL | NULL |

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

+--------+----------------------+------------+---------------+

10 rows in set (0.00 sec)

mysql> select * from customer order by car_name desc, purchase_year;

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C11 | KIA Carens | NULL | NULL |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |


| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

+--------+----------------------+------------+---------------+

10 rows in set (0.00 sec)

Using the rand() to randomize records:

mysql> select * from customer order by rand();

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C11 | KIA Carens | NULL | NULL |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

+--------+----------------------+------------+---------------+

10 rows in set (0.00 sec)

Using the alias (AS) function:

mysql> select custid as id, car_name as car, engine_id as engine, purchase_year as purchase from
customer;

+-----+----------------------+------------+----------+

| id | car | engine | purchase |

+-----+----------------------+------------+----------+

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |


| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C11 | KIA Carens | NULL | NULL |

+-----+----------------------+------------+----------+

10 rows in set (0.00 sec)

Concat function:

mysql> select concat(car_name, ',', engine_id) as 'cars', custid from customer;

+---------------------------------+--------+

| cars | custid |

+---------------------------------+--------+

| Mahindra Scorpio-N,75POY61945 | C02 |

| Toyota Fortuner,86T0271464 | C03 |

| Mahindra Thar,70QRA82590 | C04 |

| Hyundai i20,30BXP62481 | C05 |

| Toyota Yaris,51XCF52945 | C06 |

| Honda Jazz,26PYI62012 | C07 |

| Mahindra XUV700,70QPC96101 | C08 |

| Hyundai Creta,45CXA75019 | C09 |

| Toyota Innova Crysta,82TOX81965 | C10 |

| NULL | C11 |

+---------------------------------+--------+

10 rows in set (0.00 sec)

Group by function:

mysql> select * from customer group by car_name;

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |


| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C05 | Hyundai i20 | 30BXP62481 | 2018 |

| C06 | Toyota Yaris | 51XCF52945 | 2019 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |

| C11 | KIA Carens | NULL | NULL |

+--------+----------------------+------------+---------------+

10 rows in set (0.00 sec)

Count(*) function:

mysql> select count(*) as 'total' from customer;

+-------+

| total |

+-------+

| 10 |

+-------+

1 row in set (0.00 sec)

Having Command:

mysql> select * from customer having purchase_year>=2020;

+--------+----------------------+------------+---------------+

| custid | car_name | engine_id | purchase_year |

+--------+----------------------+------------+---------------+

| C02 | Mahindra Scorpio-N | 75POY61945 | 2022 |

| C03 | Toyota Fortuner | 86T0271464 | 2021 |

| C04 | Mahindra Thar | 70QRA82590 | 2021 |

| C07 | Honda Jazz | 26PYI62012 | 2020 |

| C08 | Mahindra XUV700 | 70QPC96101 | 2022 |

| C09 | Hyundai Creta | 45CXA75019 | 2022 |

| C10 | Toyota Innova Crysta | 82TOX81965 | 2022 |


+--------+----------------------+------------+---------------+

7 rows in set (0.00 sec)

Creating a new table:

mysql> create table car(engine_id varchar(10) primary key, car_name varchar(25), service_date
varchar(12), ticket_no varchar(5));

Query OK, 0 rows affected (0.04 sec)

Describe the fields of the table:

mysql> describe car;

+--------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------------+-------------+------+-----+---------+-------+

| engine_id | varchar(10) | NO | PRI | NULL | |

| car_name | varchar(25) | YES | | NULL | |

| service_date | varchar(12) | YES | | NULL | |

| ticket_no | varchar(5) | YES | | NULL | |

+--------------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

Inserting the values:

mysql> insert into car values('75POY61945', 'Mahindra Scorpio-N', '27/10/22', 'T001');

Query OK, 1 row affected (0.01 sec)

mysql> insert into car values('75POY61946', 'Mahindra Scorpio-N', ‘29/11/22’, 'T002'),


('86TOZ71464', 'Toyota Fortuner', '15/11/22', 'T003'), ('70QRA82590', 'Mahindra Thar', '15/11/22',
'T004'), ('30BXP62481', 'Hyundai i20', '30/10/22', 'T005'), ('51XCF52945', 'Toyota Yaris', '25/11/22',
'T006'), ('26PYI62012', 'Honda Jazz', '20/10/22', 'T007'), ('70QPC96101', 'Mahindra XUV700',
'25/10/22', 'T008'), ('45CXA75019', 'Hyundai Creta', '21/11/22', 'T009'), ('82TOX81965', 'Toyota
Innova Crysta', '22/11/22', 'T010');

Query OK, 8 rows affected (0.01 sec)

Records: 8 Duplicates: 0 Warnings: 0

Alter command and joining the two tables using the foreign key :

mysql> alter table customer add foreign key(engine_id) references car(engine_id);

Query OK, 10 rows affected (0.10 sec)

Records: 10 Duplicates: 0 Warnings: 0

Selecting records from both the tables after joining them:


mysql> select customer.custid as 'custid', customer.car_name as 'carname', car.service_date as
'service' from customer join car on customer.engine_id=car.engine_id;

+--------+----------------------+----------+

| custid | carname | service |

+--------+----------------------+----------+

| C01 | Honda City | 27/10/22 |

| C02 | Mahindra Scorpio-N | 29/10/22 |

| C03 | Toyota Fortuner | 15/11/22 |

| C04 | Mahindra Thar | 15/11/22 |

| C05 | Hyundai i20 | 30/10/22 |

| C06 | Toyota Yaris | 25/11/22 |

| C07 | Honda Jazz | 20/10/22 |

| C08 | Mahindra XUV700 | 25/10/22 |

| C09 | Hyundai Creta | 21/11/22 |

| C10 | Toyota Innova Crysta | 22/11/22 |

+--------+----------------------+----------+

10 rows in set (0.00 sec)

Creating a new table for using select and insert commands:

create table workers(id int, name varchar(20), dob DATE, primary key(id));

Query OK, 0 rows affected (0.04 sec)

insert into workers values(101, 'Kumar', '1976-07-05'), (102, 'Rahul', '1990-10-09'), (103, 'Harsh',
'1984-11-10');

Query OK, 3 rows affected (0.01 sec)

Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from workers where dob between '1984-05-07' and '1991-08-06';

+-----+-------+------------+

| id | name | dob |

+-----+-------+------------+

| 102 | Rahul | 1990-10-09 |

| 103 | Harsh | 1984-11-10 |

+-----+-------+------------+

mysql> select * from workers order by year(dob);


+-----+-------+------------+

| id | name | dob |

+-----+-------+------------+

| 101 | Kumar | 1976-07-05 |

| 103 | Harsh | 1984-11-10 |

| 102 | Rahul | 1990-10-09 |

+-----+-------+------------+

3 rows in set (0.01 sec)

Extracting part of a date/time:

mysql> select year(now()), month(now()), day(now()), hour(now()), minute(now()), second(now());

+-------------+--------------+------------+-------------+---------------+---------------+

| year(now()) | month(now()) | day(now()) | hour(now()) | minute(now()) | second(now()) |

+-------------+--------------+------------+-------------+---------------+---------------+

| 2022 | 10 | 19 | 20 | 15 | 49 |

+-------------+--------------+------------+-------------+---------------+---------------+

1 row in set (0.01 sec)

Creating a table and using the update commands:

mysql> create table acc(name varchar(30), balance decimal(10,2));

Query OK, 0 rows affected (0.04 sec)

mysql> insert into acc values ('Ravi', 19000), ('Aarush', 20000), ('Neha', 27000);

Query OK, 3 rows affected (0.01 sec)

Records: 3 Duplicates: 0 Warnings: 0

Selecting all records:

mysql> select * from acc;

+--------+----------+

| name | balance |

+--------+----------+

| Ravi | 19000.00 |

| Aarush | 20000.00 |

| Neha | 27000.00 |

+--------+----------+
3 rows in set (0.00 sec)

Using Update command:

mysql> update acc set balance=balance+10000 where name='Ravi';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> update acc set balance=balance-10000 where name='Neha';

Query OK, 1 row affected (0.02 sec)

Rows matched: 1 Changed: 1 Warnings: 0

Inner join:

Creating table1 and table2:

mysql> create table table1(sno int primary key, x varchar(10));

Query OK, 0 rows affected (0.04 sec)

mysql> create table table2(sno int primary key, x varchar(10));

Query OK, 0 rows affected (0.04 sec)

Inserting values into both tables:

mysql> insert into table1 values(1, 'Hyderabad'), (2, 'Delhi'), (3, 'Bangalore'), (4, 'Mumbai');

Query OK, 4 rows affected (0.01 sec)

Records: 4 Duplicates: 0 Warnings: 0

mysql> insert into table2 values(3, 'Rajasthan'), (4, 'Gujarat'), (5, 'Goa');

Query OK, 3 rows affected (0.01 sec)

Records: 3 Duplicates: 0 Warnings: 0

Selecting records from both tables using inner join:

mysql> select * from table1 inner join table2;

+-----+-----------+-----+-----------+

| sno | x | sno | x |

+-----+-----------+-----+-----------+

| 1 | Hyderabad | 5 | Goa |

| 1 | Hyderabad | 4 | Gujarat |

| 1 | Hyderabad | 3 | Rajasthan |
| 2 | Delhi | 5 | Goa |

| 2 | Delhi | 4 | Gujarat |

| 2 | Delhi | 3 | Rajasthan |

| 3 | Bangalore | 5 | Goa |

| 3 | Bangalore | 4 | Gujarat |

| 3 | Bangalore | 3 | Rajasthan |

| 4 | Mumbai | 5 | Goa |

| 4 | Mumbai | 4 | Gujarat |

| 4 | Mumbai | 3 | Rajasthan |

+-----+-----------+-----+-----------+

12 rows in set (0.00 sec)

mysql> select * from table1 inner join table2 using (x);

Empty set (0.00 sec)

mysql> select * from table1 inner join table2 using (sno);

+-----+-----------+-----------+

| sno | x |x |

+-----+-----------+-----------+

| 3 | Bangalore | Rajasthan |

| 4 | Mumbai | Gujarat |

+-----+-----------+-----------+

2 rows in set (0.00 sec)

Left and right join:

mysql> select * from table1 left join table2 on table1.sno=table2.sno;

+-----+-----------+------+-----------+

| sno | x | sno | x |

+-----+-----------+------+-----------+

| 1 | Hyderabad | NULL | NULL |

| 2 | Delhi | NULL | NULL |

| 3 | Bangalore | 3 | Rajasthan |
| 4 | Mumbai | 4 | Gujarat |

+-----+-----------+------+-----------+

4 rows in set (0.00 sec)

mysql> select * from table1 left join table2 using (x);

+-----------+-----+------+

|x | sno | sno |

+-----------+-----+------+

| Hyderabad | 1 | NULL |

| Delhi | 2 | NULL |

| Bangalore | 3 | NULL |

| Mumbai | 4 | NULL |

+-----------+-----+------+

4 rows in set (0.00 sec)

mysql> select * from table1 left join table2 using (sno);

+-----+-----------+-----------+

| sno | x |x |

+-----+-----------+-----------+

| 1 | Hyderabad | NULL |

| 2 | Delhi | NULL |

| 3 | Bangalore | Rajasthan |

| 4 | Mumbai | Gujarat |

+-----+-----------+-----------+

4 rows in set (0.00 sec)

mysql> select * from table1 right join table2 using (sno);

+-----+-----------+-----------+

| sno | x |x |

+-----+-----------+-----------+

| 3 | Rajasthan | Bangalore |
| 4 | Gujarat | Mumbai |

| 5 | Goa | NULL |

+-----+-----------+-----------+

3 rows in set (0.00 sec)

mysql> select * from table1 right join table2 using (x);

+-----------+-----+------+

|x | sno | sno |

+-----------+-----+------+

| Rajasthan | 3 | NULL |

| Gujarat | 4 | NULL |

| Goa | 5 | NULL |

+-----------+-----+------+

3 rows in set (0.00 sec)

mysql> select * from table1 right join table2 on table1.sno=table2.sno;

+------+-----------+-----+-----------+

| sno | x | sno | x |

+------+-----------+-----+-----------+

| 3 | Bangalore | 3 | Rajasthan |

| 4 | Mumbai | 4 | Gujarat |

| NULL | NULL | 5 | Goa |

+------+-----------+-----+-----------+

3 rows in set (0.00 sec)

You might also like