100% found this document useful (1 vote)
195 views

Mysql Queries

The document demonstrates how to perform various operations in MySQL like creating a database and table, inserting, updating, deleting and retrieving data from the table. It also shows how to use clauses, functions and joins. Key operations covered include creating a student table with fields id, name, gender; inserting sample records; using where, order by, group by and like clauses; and aggregate functions like count, sum, average to retrieve data from the table. Joins are also introduced with the creation of an employee table.

Uploaded by

amdileeyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
195 views

Mysql Queries

The document demonstrates how to perform various operations in MySQL like creating a database and table, inserting, updating, deleting and retrieving data from the table. It also shows how to use clauses, functions and joins. Key operations covered include creating a student table with fields id, name, gender; inserting sample records; using where, order by, group by and like clauses; and aggregate functions like count, sum, average to retrieve data from the table. Joins are also introduced with the creation of an employee table.

Uploaded by

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

To create Database:

mysql> create database cegon;


Query OK, 1 row affected (0.00 sec)
To use the Database:
mysql> use cegon;
Database changed
To create Table:
mysql> create table student(id
varchar(10),primary key(id));
Query OK, 0 rows affected (0.03 sec)

int(10),name

varchar(20),gender

To view the structure of the table:


mysql> describe student;
+--------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id
| int(10)
| NO | PRI | 0
|
|
| name | varchar(20) | YES | | NULL |
|
| gender | varchar(10) | YES | | NULL |
|
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
To Insert the data:
mysql> insert into student values(224,'mohanraj','male');
Query OK, 1 row affected (0.03 sec)
mysql> insert into student values(225,'karthi','male');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(226,'jayashree','female');
Query OK, 1 row affected (0.00 sec)

To retrieve the data


mysql> select * from student;
+-----+-----------+--------+
| id | name
| gender |
+-----+-----------+--------+
| 224 | mohanraj | male |
| 225 | karthi | male |
| 226 | jayashree | female |
+-----+-----------+--------+
3 rows in set (0.00 sec)
How to use where clause?
mysql> select * from student where name='mohanraj';
+-----+----------+--------+
| id | name | gender |
+-----+----------+--------+
| 224 | mohanraj | male |
+-----+----------+--------+
1 row in set (0.02 sec)
mysql> select id,name from student where name='mohanraj';
+-----+----------+
| id | name |
+-----+----------+
| 224 | mohanraj |
+-----+----------+
1 row in set (0.00 sec)
How to use alter command?
mysql> alter table student add column email varchar(20);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> describe student;
+--------+-------------+------+-----+----------------+-------+
| Field | Type
| Null | Key | Default | Extra |

+--------+-------------+------+-----+--------+-------+
| id
| int(10)
| NO | PRI | 0
|
|
| name | varchar(20) | YES |
| NULL |
|
| gender | varchar(10) | YES |
| NULL |
|
| email | varchar(20) | YES |
| NULL |
|
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from student;
+-----+-----------+--------+-------+
| id | name
| gender | email |
+-----+-----------+--------+-------+
| 224 | mohanraj | male | NULL |
| 225 | karthi | male | NULL |
| 226 | jayashree | female | NULL |
+-----+-----------+--------+-------+
3 rows in set (0.00 sec)
How to update the data?
mysql> update student set email='raj@gmail.com' where id=224;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set email='karthi@gmail.com' where id=225;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set email='jayashree@gmail.com' where id=226;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+-----+-----------+--------+---------------------+
| id | name
| gender | email
|
+-----+-----------+--------+---------------------+
| 224 | mohanraj | male | raj@gmail.com
|
| 225 | karthi | male | karthi@gmail.com |
| 226 | jayashree | female | jayashree@gmail.com |

+-----+-----------+--------+---------------------+
3 rows in set (0.00 sec)
After updating how to insert the data?
mysql> insert into student values(227,'venu','female','venu@gmail.com');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+-----+-----------+--------+---------------------+
| id | name
| gender | email
|
+-----+-----------+--------+---------------------+
| 224 | mohanraj | male | raj@gmail.com
|
| 225 | karthi | male | karthi@gmail.com |
| 226 | jayashree | female | jayashree@gmail.com |
| 227 | venu
| female | venu@gmail.com
|
+-----+-----------+--------+---------------------+
4 rows in set (0.00 sec)
How to delete the data?
mysql> delete from student where id=227;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+-----+-----------+--------+---------------------+
| id | name
| gender | email
|
+-----+-----------+--------+---------------------+
| 224 | mohanraj | male | raj@gmail.com
|
| 225 | karthi | male | karthi@gmail.com |
| 226 | jayashree | female | jayashree@gmail.com |
+-----+-----------+--------+---------------------+
3 rows in set (0.00 sec)
How to use like ?
mysql> select * from student where name like 'ja%';
+-----+-----------+--------+---------------------+
| id | name
| gender | email
|
+-----+-----------+--------+---------------------+
| 226 | jayashree | female | jayashree@gmail.com |
+-----+-----------+--------+---------------------+

1 row in set (0.00 sec)


mysql> select * from student where name like '%ja';
Empty set (0.00 sec)
mysql> select * from student where name like '%hi';
+-----+--------+--------+------------------+
| id | name | gender | email
|
+-----+--------+--------+------------------+
| 225 | karthi | male | karthi@gmail.com |
+-----+--------+--------+------------------+
1 row in set (0.00 sec)
Order by:
mysql> select id,name,gender from student order by gender;
+-----+-----------+--------+
| id | name
| gender |
+-----+-----------+--------+
| 226 | jayashree | female |
| 224 | mohanraj | male |
| 225 | karthi | male |
+-----+-----------+--------+
3 rows in set (0.00 sec)
mysql> select id,name,gender from student order by gender asc;
+-----+-----------+--------+
| id | name
| gender |
+-----+-----------+--------+
| 226 | jayashree | female |
| 224 | mohanraj | male |
| 225 | karthi | male |
+-----+-----------+--------+
3 rows in set (0.00 sec)

mysql> select id,name,gender from student order by gender desc;


+-----+-----------+--------+
| id | name
| gender |
+-----+-----------+--------+
| 224 | mohanraj | male |
| 225 | karthi | male |
| 226 | jayashree | female |
+-----+-----------+--------+
3 rows in set (0.00 sec)
Group by:
mysql> select id,name,gender from student group by gender desc;
+-----+-----------+--------+
| id | name
| gender |
+-----+-----------+--------+
| 224 | mohanraj | male |
| 226 | jayashree | female |
+-----+-----------+--------+
2 rows in set (0.02 sec)
mysql> select id,name,gender from student group by gender asc;
+-----+-----------+--------+
| id | name
| gender |
+-----+-----------+--------+
| 226 | jayashree | female |
| 224 | mohanraj | male |
+-----+-----------+--------+
2 rows in set (0.00 sec)
Aggregate functions:
Count:
mysql> select count(name) from student;
+-------------+
| count(name) |
+-------------+
|
3|
+-------------+
1 row in set (0.02 sec)
mysql> select count(name) from student group by gender;

+-------------+
| count(name) |
+-------------+
|
1|
|
2|
+-------------+
2 rows in set (0.01 sec)
mysql> select count(name) from student group by name;
+-------------+
| count(name) |
+-------------+
|
1|
|
1|
|
1|
+-------------+
3 rows in set (0.00 sec)
mysql> alter table student add column fee int(10);
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> update student set fee=1000 where id=224;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set fee=2000 where id=225;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set fee=3000 where id=226;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student;


+-----+-----------+--------+---------------------+------+
| id | name
| gender | email
| fee |
+-----+-----------+--------+---------------------+------+
| 224 | mohanraj | male | raj@gmail.com
| 1000 |
| 225 | karthi | male | karthi@gmail.com | 2000 |
| 226 | jayashree | female | jayashree@gmail.com | 3000 |
+-----+-----------+--------+---------------------+------+
3 rows in set (0.00 sec)
SUM:
mysql> select sum(fee) from student;
+----------+
| sum(fee) |
+----------+
| 6000 |
+----------+
1 row in set (0.01 sec)
mysql> select sum(fee) from student group by gender;
+----------+
| sum(fee) |
+----------+
| 3000 |
| 3000 |
+----------+
2 rows in set (0.02 sec)
mysql> select sum(fee) from student order by gender;
+----------+
| sum(fee) |
+----------+
| 6000 |
+----------+
1 row in set (0.00 sec)

mysql> select gender,sum(fee) from student order by gender;


+--------+----------+
| gender | sum(fee) |
+--------+----------+
| male | 6000 |
+--------+----------+
1 row in set (0.00 sec)
mysql> select gender,sum(fee) from student group by gender;
+--------+----------+
| gender | sum(fee) |
+--------+----------+
| female | 3000 |
| male | 3000 |
+--------+----------+
2 rows in set (0.00 sec)

mysql>insert into student


values(228,'venu','female','venu@gmail.com',4000);
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+-----+-----------+--------+---------------------+------+
| id | name
| gender | email
| fee |
+-----+-----------+--------+---------------------+------+
| 224 | mohanraj | male | raj@gmail.com
| 1000 |
| 225 | karthi | male | karthi@gmail.com | 2000 |
| 226 | jayashree | female | jayashree@gmail.com | 3000 |
| 228 | venu
| female | venu@gmail.com
| 4000 |
+-----+-----------+--------+---------------------+------+
4 rows in set (0.00 sec)

AVERAGE:
mysql> select avg(fee) from student;
+-----------+
| avg(fee) |
+-----------+
| 2500.0000 |
+-----------+
1 row in set (0.00 sec)
MINIMUM:
mysql> select min(fee) from student;
+----------+
| min(fee) |
+----------+
| 1000 |
+----------+
1 row in set (0.00 sec)
MAXIMUM:
mysql> select max(fee) from student;
+----------+
| max(fee) |
+----------+
| 4000 |
+----------+
1 row in set (0.00 sec)
-------------------------------------------------------------------------------------------JOINS:
mysql> create table emp(eid int(10),ename varchar(20),salary int(10));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into emp values(223,'mohanraj',20000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values(224,'fizur',25000);
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values(225,'Rizwan',25000);


Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values(226,'selvi',23000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;


+------+----------+--------+
| eid | ename | salary |
+------+----------+--------+
| 223 | mohanraj | 20000 |
| 224 | fizur | 25000 |
| 225 | Rizwan | 25000 |
| 226 | selvi | 23000 |
+------+----------+--------+
4 rows in set (0.00 sec)
NORMAL JOINS:
mysql> select a.id,a.name,b.ename,b.salary from student a,emp b where
a.name=b.ename;
+-----+----------+----------+--------+
| id | name | ename | salary |
+-----+----------+----------+--------+
| 224 | mohanraj | mohanraj | 20000 |
+-----+----------+----------+--------+
1 row in set (0.01 sec)
OUTERJOIN:
LEFT OUTER JOIN:
mysql> select a.id,a.name,b.ename,b.salary from student a left join emp
b on a.name=b.ename;
+-----+-----------+----------+--------+
| id | name
| ename | salary |
+-----+-----------+----------+--------+
| 224 | mohanraj | mohanraj | 20000 |
| 225 | karthi | NULL | NULL |

| 226 | jayashree | NULL | NULL |


| 228 | venu
| NULL | NULL |
+-----+-----------+----------+--------+
4 rows in set (0.00 sec)

RIGHT OUTER JOIN:


mysql> select a.id,a.name,b.ename,b.salary from student a right join
emp b on a.name=b.ename;
+------+----------+----------+--------+
| id | name | ename | salary |
+------+----------+----------+--------+
| 224 | mohanraj | mohanraj | 20000 |
| NULL | NULL | fizur | 25000 |
| NULL | NULL | Rizwan | 25000 |
| NULL | NULL | selvi | 23000 |
+------+----------+----------+--------+
4 rows in set (0.00 sec)
INNER JOIN:
mysql> select a.id,a.name,b.ename,b.salary from student a inner join
emp b on a.name=b.ename;
+-----+----------+----------+--------+
| id | name | ename | salary |
+-----+----------+----------+--------+
| 224 | mohanraj | mohanraj | 20000 |
+-----+----------+----------+--------+
1 row in set (0.00 sec)
mysql> select a.id,a.name,b.ename,b.salary from student a inner join
emp b on a.id=b.eid;
+-----+-----------+--------+--------+
| id | name
| ename | salary |
+-----+-----------+--------+--------+
| 224 | mohanraj | fizur | 25000 |
| 225 | karthi | Rizwan | 25000 |
| 226 | jayashree | selvi | 23000 |

+-----+-----------+--------+--------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+------+----------+--------+
| eid | ename | salary |
+------+----------+--------+
| 223 | mohanraj | 20000 |
| 224 | fizur | 25000 |
| 225 | Rizwan | 25000 |
| 226 | selvi | 23000 |
+------+----------+--------+
4 rows in set (0.00 sec)
Timestamp in Mysql
mysql> select sysdate();
+---------------------+
| sysdate()
|
+---------------------+
| 2009-07-30 18:23:08 |
+---------------------+
1 row in set (0.02 sec)
Date in Mysql
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2009-07-30 |
+------------+
1 row in set (0.00 sec)
Time in Mysql
mysql> select utc_time();
+------------+
| utc_time() |
+------------+

| 13:00:40 |
+------------+
1 row in set (0.01 sec)
AND and OR :
mysql> select * from student where name='mohanraj' or name='karthi';
+-----+----------+--------+------------------+------+
| id | name | gender | email
| fee |
+-----+----------+--------+------------------+------+
| 224 | mohanraj | male | raj@gmail.com | 1000 |
| 225 | karthi | male | karthi@gmail.com | 2000 |
+-----+----------+--------+------------------+------+
2 rows in set (0.00 sec)
mysql> select *
name='karthi';
Empty set (0.00 sec)

from

student

where

name='mohanraj'

and

mysql> select *
email='karthi';
Empty set (0.00 sec)

from

student

where

name='mohanraj'

and

mysql> select * from student where name='mohanraj'


email='raj@gmail.com';
+-----+----------+--------+---------------+------+
| id | name | gender | email
| fee |
+-----+----------+--------+---------------+------+
| 224 | mohanraj | male | raj@gmail.com | 1000 |
+-----+----------+--------+---------------+------+
1 row in set (0.00 sec)

and

IN:
mysql>insert into student
values(229,'laxmi','female','laxmi@gmail.com',1000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student
values(230,'rajini','male','rajini@gmail.com',2000);

Query OK, 1 row affected (0.00 sec)


mysql> select * from student;
+-----+-----------+--------+---------------------+------+
| id | name
| gender | email
| fee |
+-----+-----------+--------+---------------------+------+
| 224 | mohanraj | male | raj@gmail.com
| 1000 |
| 225 | karthi | male | karthi@gmail.com | 2000 |
| 226 | jayashree | female | jayashree@gmail.com | 3000 |
| 228 | venu
| female | venu@gmail.com
| 4000 |
| 229 | laxmi | female | laxmi@gmail.com | 1000 |
| 230 | rajini | male | rajini@gmail.com | 2000 |
mysql> select name,email from student where fee in(1000,2000);
+----------+------------------+
| name | email
|
+----------+------------------+
| mohanraj | raj@gmail.com |
| karthi | karthi@gmail.com |
| laxmi | laxmi@gmail.com |
| rajini | rajini@gmail.com |
+----------+------------------+
4 rows in set (0.00 sec)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy