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

My SQL: #To Show All Databses Availaible

The document demonstrates various SQL commands for creating databases and tables, inserting, updating, selecting and ordering data in MySQL. It shows how to create a database and use it, create and populate a table, insert rows, select rows based on conditions, order and group data, use functions like COUNT, DISTINCT, SUBSTRING and UPPERCASE. Various WHERE clause operations like LIKE, IS NULL, BETWEEN are also demonstrated to retrieve selective rows from tables.

Uploaded by

Aastha Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

My SQL: #To Show All Databses Availaible

The document demonstrates various SQL commands for creating databases and tables, inserting, updating, selecting and ordering data in MySQL. It shows how to create a database and use it, create and populate a table, insert rows, select rows based on conditions, order and group data, use functions like COUNT, DISTINCT, SUBSTRING and UPPERCASE. Various WHERE clause operations like LIKE, IS NULL, BETWEEN are also demonstrated to retrieve selective rows from tables.

Uploaded by

Aastha Bansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MY SQL

#TO SHOW ALL DATABSES AVAILAIBLE


mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.11 sec)

mysql> use test


Database changed
mysql> create table student(admission_no int(5),
-> -> name varchar(15),
-> -> age int(2)
->
->
->
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near '-> name varchar(15),
-> age int(2)' at line 2

#HOW TO CREATE A TABLE


mysql> use test
Database changed
mysql> create table student(admission_no int(5),
-> name varchar(15),
-> age int(2),
-> city varchar(30));
Query OK, 0 rows affected (0.20 sec)

mysql> select*from student


-> ;
Empty set (0.01 sec)

#INSERTING VALUES INTO THE TABLE

mysql> insert into student values(100,"riya",14,"agra");


Query OK, 1 row affected (0.14 sec)

mysql> insert into student values(101,"anu",13,"noida");


Query OK, 1 row affected (0.16 sec)

mysql> insert into student values(102,"shiv",15,"aligarh");


Query OK, 1 row affected (0.05 sec)

#DISPLAYING THE TABLE WITH INSERTED VALUES

mysql> select*from student


->
-> ;
+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 100 | riya | 14 | agra |
| 101 | anu | 13 | noida |
| 102 | shiv | 15 | aligarh |
+--------------+------+------+---------+
3 rows in set (0.00 sec)

#PUTTING IN ASCENDING ORDER

mysql> select*from student order by name;


+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 101 | anu | 13 | noida |
| 100 | riya | 14 | agra |
| 102 | shiv | 15 | aligarh |
+--------------+------+------+---------+
3 rows in set (0.00 sec)

#PUTTING IN DESCENDING ORDER

mysql> select*from student order by name desc;


+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 102 | shiv | 15 | aligarh |
| 100 | riya | 14 | agra |
| 101 | anu | 13 | noida |
+--------------+------+------+---------+
3 rows in set (0.00 sec)

mysql> insert into student(name,age) values("mona",18);


Query OK, 1 row affected (0.16 sec)

mysql> select*from student;


+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 100 | riya | 14 | agra |
| 101 | anu | 13 | noida |
| 102 | shiv | 15 | aligarh |
| NULL | mona | 18 | NULL |
+--------------+------+------+---------+
4 rows in set (0.00 sec)
#SELECTING ACCORDING TO GIVEN CONDITION
#Selecting The name of students whose name starts from a
#way 1
mysql> select*from student where name like "a%";
+--------------+------+------+-------+
| admission_no | name | age | city |
+--------------+------+------+-------+
| 101 | anu | 13 | noida |
+--------------+------+------+-------+
1 row in set (0.10 sec)
#way2
mysql> select*from student where name like "a__";
+--------------+------+------+-------+
| admission_no | name | age | city |
+--------------+------+------+-------+
| 101 | anu | 13 | noida |
+--------------+------+------+-------+
1 row in set (0.00 sec)

mysql> select*from student order by age desc,name;


+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| NULL | mona | 18 | NULL |
| 102 | shiv | 15 | aligarh |
| 100 | riya | 14 | agra |
| 101 | anu | 13 | noida |
+--------------+------+------+---------+
4 rows in set (0.00 sec)

#CHANGING (UPDATING THE VALUES)


#USING UPDATE FUNCTION
#SYNTAX- UPDATE <TABLE> SET WHERE <REQUIRED CONDITIONS>

mysql> update student set age=17 where city="agra";


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

mysql> select*from student


-> ;
+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 100 | riya | 17 | agra |
| 101 | anu | 13 | noida |
| 102 | shiv | 15 | aligarh |
| NULL | mona | 18 | NULL |
+--------------+------+------+---------+
4 rows in set (0.00 sec)

#SELECTING PARTICULAR ROW(S) OR COULOUMN(S) FROM THE GIVEN TABLE


#USING DISTINCT FUNCTION

mysql> select distinct name from student;


+------+
| name |
+------+
| riya |
| anu |
| shiv |
| mona |
+------+
4 rows in set (0.03 sec)

mysql> select city from student;


+---------+
| city |
+---------+
| agra |
| noida |
| aligarh |
| NULL |
+---------+
4 rows in set (0.00 sec)

mysql> select distinct city from student;


+---------+
| city |
+---------+
| agra |
| noida |
| aligarh |
| NULL |
+---------+
4 rows in set (0.00 sec)

mysql> select distinct city from student;


+---------+
| city |
+---------+
| agra |
| noida |
| aligarh |
| NULL |
+---------+
4 rows in set (0.00 sec)

#USING IS AND IS NOT

mysql> select*from student where admission_no is null;


+--------------+------+------+------+
| admission_no | name | age | city |
+--------------+------+------+------+
| NULL | mona | 18 | NULL |
+--------------+------+------+------+
1 row in set (0.00 sec)

mysql> select*from student where city is not null;


+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 100 | riya | 17 | agra |
| 101 | anu | 13 | noida |
| 102 | shiv | 15 | aligarh |
+--------------+------+------+---------+
3 rows in set (0.00 sec)

mysql> update student set city="agra" where age=18;


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

mysql> select*from student;


+--------------+------+------+---------+
| admission_no | name | age | city |
+--------------+------+------+---------+
| 100 | riya | 17 | agra |
| 101 | anu | 13 | noida |
| 102 | shiv | 15 | aligarh |
| NULL | mona | 18 | agra |
+--------------+------+------+---------+
4 rows in set (0.00 sec)

mysql>

#CREATION OF A NEW TABLE


mysql> use test
Database changed
mysql> create table sports (student_no int(2),
-> class int(1),
-> name varchar(10),
-> game1 varchar(15),
-> grade varchar(1),
-> game2 varchar(15),
-> grade2 varchar(1));
Query OK, 0 rows affected (0.21 sec)
#INSERTING VALUES
mysql> insert into sports value(10,7,"sammer","cricket","B","Swimming","A");
Query OK, 1 row affected (0.14 sec)

mysql> insert into sports value(11,8,"sujit","tennis","A","Skating","C");


Query OK, 1 row affected (0.14 sec)

mysql> insert into sports value(12,7,"kamal","swimmimg","B","Football","B");


Query OK, 1 row affected (0.07 sec)

mysql> insert into sports value(13,7,"venna","tennis","C","Tennis","A");


Query OK, 1 row affected (0.06 sec)

mysql> select*from sports


-> ;
+------------+-------+--------+----------+-------+----------+--------+
| student_no | class | name | game1 | grade | game2 | grade2 |
+------------+-------+--------+----------+-------+----------+--------+
| 10 | 7 | sammer | cricket | B | Swimming | A |
I 11 | 8 | sujit | tennis | A | Skating | C |
| 12 | 7 | kamal | swimmimg | B | Football | B |
| 13 | 7 | venna | tennis | C | Tennis | A |
+------------+-------+--------+----------+-------+----------+--------+
4 rows in set (0.05 sec)
#GIVING MULTIPLE CONDITION THROUGH OR
mysql> select name from sports where grade1='C' or grade2='C';
ERROR 1054 (42S22): Unknown column 'grade1' in 'where clause'
mysql> select name from sports where grade='C' or grade2='C';
+-------+
| name |
+-------+
| sujit |
| venna |
+-------+
2 rows in set (0.06 sec)

mysql> select count(*) from sports where (game1='cricket'and grade='A')or (game2='cricket'and grade2='A');
+----------+
| count(*) |
+----------+
| 0|
+----------+
1 row in set (0.04 sec)

mysql> select*from sports where name like "A__";


Empty set (0.20 sec)

mysql> select*from sports where name like"S__";


Empty set (0.00 sec)

mysql> select*from sports where name like "s_____";


+------------+-------+--------+---------+-------+----------+--------+
| student_no | class | name | game1 | grade | game2 | grade2 |
+------------+-------+--------+---------+-------+----------+--------+
| 10 | 7 | sammer | cricket | B | Swimming | A |
+------------+-------+--------+---------+-------+----------+--------+
1 row in set (0.00 sec)

mysql> select*from sports where name like "s%";


+------------+-------+--------+---------+-------+----------+--------+
| student_no | class | name | game1 | grade | game2 | grade2 |
+------------+-------+--------+---------+-------+----------+--------+
| 10 | 7 | sammer | cricket | B | Swimming | A |
| 11 | 8 | sujit | tennis | A | Skating | C |
+------------+-------+--------+---------+-------+----------+--------+
2 rows in set (0.00 sec)

mysql> select*from sports where name like "v%";


+------------+-------+-------+--------+-------+--------+--------+
| student_no | class | name | game1 | grade | game2 | grade2 |
+------------+-------+-------+--------+-------+--------+--------+
| 13 | 7 | venna | tennis | C | Tennis | A |
+------------+-------+-------+--------+-------+--------+--------+
1 row in set (0.00 sec)

mysql>
SQL FUNCTIONS

1. UPPER CASE mysql> select ucase (name) from student;


+--------------+
| ucase (name) |
+--------------+
| RIYA |
| ANU |
| SHIV |
| MONA |
+--------------+
4 rows in set (0.14 sec)

2. SELECTS SUBSTRING FROM THE GIVEN STRING mysql> select substr(name,3,4) from student;
+------------------+
| substr(name,3,4) |
+------------------+
| ya |
|u |
| iv |
| na |
+------------------+
4 rows in set (0.03 sec)

3. JOINING ANY TWO STRINGS mysql> select concat(name,city) from student;


+-------------------+
| concat(name,city) |
+-------------------+
| riyaagra |
| anunoida |
| shivaligarh |
| monaagra |
+-------------------+
4 rows in set (0.03 sec)

mysql> select substr(name,-4,4) from student;


+-------------------+
| substr(name,-4,4) |
+-------------------+
| riya |
| |
| shiv |
| mona |
+-------------------+
4 rows in set (0.03 sec)

4. SELECTING FROM LEFT SIDE OF THE GIVEN STRING UPTO THE GIVEN PLACE
mysql> select left("name",2);
+----------------+
| left("name",2) |
+----------------+
| na |
+----------------+
1 row in set (0.04 sec)
mysql> select left (name,2) from student;
+---------------+
| left (name,2) |
+---------------+
| ri |
| an |
| sh |
| mo |
+---------------+
4 rows in set (0.00 sec)

5. FINDING THE LENGTH OF THE STRING AND SUBSTRING


mysql> select length (name) from student;
+---------------+
| length (name) |
+---------------+
| 4|
| 3|
| 4|
| 4|
+---------------+
4 rows in set (0.07 sec)

6. REMOVING TRAILING SPACES


mysql> select trim (" hello ");
ERROR 1630 (42000): FUNCTION test.trim does not exist. Check the 'Function Name Parsing and Resolution' section
in the Reference Manual
mysql> select trim(" hello ");
+---------------------+
| trim(" hello ") |
+---------------------+
| hello |
+---------------------+
1 row in set (0.04 sec)

7. FINDING SUBSTRING FROM THE GIVEN STRING


mysql> select instr("hellooooo","cd");
+-------------------------+
| instr("hellooooo","cd") |
+-------------------------+
| 0|
+-------------------------+
1 row in set (0.07 sec)

mysql> select instr("helllloooo","lo");


+--------------------------+
| instr("helllloooo","lo") |
+--------------------------+
| 6|
+--------------------------+
1 row in set (0.00 sec)

mysql> select ltrim(" hello");


+-----------------------+
| ltrim(" hello") |
+-----------------------+
| hello |
+-----------------------+
1 row in set (0.12 sec)
*MATH FUNCTIONS
1. MODULUS

mysql> select mod(15,2);


+-----------+
| mod(15,2) |
+-----------+
| 1|
+-----------+
1 row in set (0.03 sec)

2. SQAUREROOT

mysql> select sqrt(8);


+--------------------+
| sqrt(8) |
+--------------------+
| 2.8284271247461903 |
+--------------------+
1 row in set (0.07 sec)

3. EXPOTENTIAL FUNCTION OF POWER

mysql> select pow(2,3);


+----------+
| pow(2,3) |
+----------+
| 8|
+----------+
1 row in set (0.00 sec)

#ROUNDING OFF

mysql> select round(15.5678);


+----------------+
| round(15.5678) |
+----------------+
| 16 |
+----------------+
1 row in set (0.00 sec)
#ROUNDING OFF UPTO TO THE NEAREST HUNDRED TILL TWO DECIMAL PLACES

mysql> select round(15.4678,2);


+------------------+
| round(15.4678,2) |
+------------------+
| 15.47 |
+------------------+
1 row in set (0.00 sec)
#ROUNDING OFF ONLY THE INTEGER AND REMOVING THE DECIMAL

mysql> select round(15.678,-1);


+------------------+
| round(15.678,-1) |
+------------------+
| 20 |
+------------------+
1 row in set (0.00 sec)

mysql> select round(144.5678,-1);


+--------------------+
| round(144.5678,-1) |
+--------------------+
| 140 |
+--------------------+
1 row in set (0.00 sec)

mysql> select round(144.5678,-2);


+--------------------+
| round(144.5678,-2) |
+--------------------+
| 100 |
+--------------------+
1 row in set (0.00 sec)

mysql> select round(19870.5678,-3);


+----------------------+
| round(19870.5678,-3) |
+----------------------+
| 20000 |
+----------------------+
1 row in set (0.00 sec)

#TRUNCATE REMOVES THE DECIMAL AND GIVES BACK THE INTEGER PART

mysql> select truncate(14.45556,0);


+----------------------+
| truncate(14.45556,0) |
+----------------------+
| 14 |
+----------------------+
1 row in set (0.00 sec)

mysql> select truncate(15.354,-1);


+---------------------+
| truncate(15.354,-1) |
+---------------------+
| 10 |
+---------------------+
1 row in set (0.00 sec)

mysql> select round(15);


+-----------+
| round(15) |
+-----------+
| 15 |
+-----------+
1 row in set (0.00 sec)

mysql> select round(15.56);


+--------------+
| round(15.56) |
+--------------+
| 16 |
+--------------+
1 row in set (0.00 sec)
*DATE FUNCTIONS

1. CURRENT DATE AND TIME


mysql> select now();

| now() |
+---------------------+
| 2021-06-24 11:34:48 |
+---------------------+
1 row in set (0.04 sec)

2. CURRENT DATE
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2021-06-24 |
+------------+
1 row in set (0.00 sec)

3. CURRENT YEAR
mysql> select year(curdate());
+-----------------+
| year(curdate()) |
+-----------------+
| 2021 |
+-----------------+
1 row in set (0.00 sec)

4. CURRENT MONTH

mysql> select month(curdate());


+------------------+
| month(curdate()) |
+------------------+
| 6|
+------------------+
1 row in set (0.00 sec)

5. CURRENT DATE

mysql> select date(curdate());


+-----------------+
| date(curdate()) |
+-----------------+
| 2021-06-24 |
+-----------------+
1 row in set (0.00 sec)

6. CURRENT DAYNAME

mysql> select dayname(curdate());


+--------------------+
| dayname(curdate()) |
+--------------------+
| Thursday |
+--------------------+
1 row in set (0.01 sec)

7. NUMBER OF DAY OF THE WEEK


mysql> select dayofweek(curdate());
+----------------------+
| dayofweek(curdate()) |
+----------------------+
| 5|
+----------------------+
1 row in set (0.00 sec)

8. CURRENT TIME AND TIME AFTER SLEEP


mysql> select sysdate(),sleep(3),sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() |
+---------------------+----------+---------------------+
| 2021-06-24 11:40:34 | 0 | 2021-06-24 11:40:37 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)

9. CURRENT TIME AND THE SAME TIME AFTER SLEEP EXECUTION


mysql> select now(),sleep(3),now();
+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2021-06-24 11:41:28 | 0 | 2021-06-24 11:41:28 |
+---------------------+----------+---------------------+
1 row in set (3.01 sec)

mysql> show tables;


+----------------+
| Tables_in_test |
+----------------+
| graduate |
| model |
| sports |
| student |
+----------------+
4 rows in set (0.16 sec)

You might also like