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

SQL Notes

This document provides an overview of SQL queries and concepts. It covers creating and selecting databases and tables, inserting and selecting data, filtering with where clauses, aggregation functions, grouping, sorting, limits and wildcards.

Uploaded by

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

SQL Notes

This document provides an overview of SQL queries and concepts. It covers creating and selecting databases and tables, inserting and selecting data, filtering with where clauses, aggregation functions, grouping, sorting, limits and wildcards.

Uploaded by

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

Lecture Title: SQL Query Lecture 1

App Notes: SQL


0. To create database in MySql
mysql>create database db_name; //Query
1.To show database in MySQL -
mysql> show databases; //Query
+--------------------------+
| Database |
+--------------------------+
crud_demo_1
crud_demo_2
cust_reg_db
customerregistrationdb_2
db_mysql
demo_practice
information_schema
login_db_demo
marketing_lead_db
marketing_lead_db_1
mysql
performance_schema
registrationdb_1
sakila
sayyed_db
student
sys
world
zohocrmapp
zohocrmapp1
+--------------------------+
2. To create table in db_name:
mysql> create table inceincentive_list(id int,incentive_amount int,date varchar(25));
//query

2.To select database in MySQL -


mysql> use crud_demo_1; //query

Database changed
3.To show tables in database -
mysql> show tables; //query
+-----------------------+
| Tables_in_crud_demo_1 |
+-----------------------+
employees
student

mysql> select * from employees; //query


Empty set (0.13 sec)

5.To describes tables in database -


mysql> desc employees; //query
+--------+-------------+------+-----+---------+-------+------+-----------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+-------+-----------+
| id | int | YES | | NULL | |
| name | varchar(45) | YES | | NULL | |
| city | varchar(45) | YES | | NULL | |
| salary | int | YES | | NULL | |

6.To insert values into tables -


mysql> insert into employees values(1,"pradeep","varanasi",2000); //query
mysql> insert into employees values(2,"vivek","ayodhya",3000); //query
mysql> insert into employees values(3,"pravesh","azamgarh",4000); //query
mysql> insert into employees values(4,"sanjay","varanasi",5000); //query

7.To read the data into tables -

mysql> select * from employees; //query


+------+---------+----------+--------+-------+--------+
| id | name | city | salary |
+------+---------+----------+--------+--------+--------+
| 1 | pradeep | varansi | 2000 |
| 2 | vivek | ayodhya | 3000 |
| 3 | pravesh | azamgarh | 4000 |
| 4 | sanjay | varansi | 5000 |
+------+---------+----------+--------+--------+--------+

8.To select the name where salary is 3000 -


mysql> select name from employees where salary=3000; //query
+-------+
| name |
+-------+
| vivek |
+-------+
9. Give me the employee name where salary is equal to or more than 3000;
mysql> select name from employees where salary>=3000; //query
+---------+
| name |
+---------+
| vivek |
| pravesh |
| sanjay |
+---------+

10. Give me the employee name where salary is 5000 and 2000 thousands;
mysql> select name from employees where salary in (5000,2000); //query
+---------+ //in operator is used when you
| name | performed equals with more than one
+---------+ data.
| pradeep |
| sanjay |
+---------+

10. Give me the employee Id whose name is sanjay and pradeep?


mysql> select id from employees where name in ("pradeep","sanjay"); //query
+------+
| id |
+------+
| 1|
| 4|
+------+

select->from->where->groupby->having->orderby /// golden rule

10. Give me the employee Id sorted in descending order?


mysql> select id from employees order by id desc; //query
+------+
| id |
+------+
| 4|
| 3|
| 2|
| 1|
+------+

In sql to sort the data we use order by command.


11. Give me the employee Id sorted in descending order?
mysql> select id from employees order by id asc; //query
+------+
| id |
+------+
| 1|
| 2|
| 3|
| 4|
+------+

12. Give me the employee name sorted in descending order?


mysql> select name from employees order by name desc; //query
+---------+
| name |
+---------+
| vivek |
| sanjay |
| pravesh |
| pradeep |
+---------+
13. Give me the employee name sorted in ascending order?
mysql> select name from employees order by name asc; //query
+---------+
| name |
+---------+
| pradeep |
| pravesh |
| sanjay |
| vivek |
+---------+

BUILT IN FUNCTION IN SQL:

14. Give me the maximum salary from the table?


mysql> select max(salary) from employees; //query
+-------------+
| max(salary) |
+-------------+
| 5000 |
+-------------+

14. Give me the minimum salary from the table?


mysql> select min(salary) from employees; //query
+-------------+
| min(salary) |
+-------------+
| 2000 |
+-------------+

15. Give me the average salary from the table?


mysql> select avg(salary) from employees; //query
+-------------+
| avg(salary) |
+-------------+
| 3500.0000 |
+-------------+

16. Give me the sum of all salaries from the table?


mysql> select sum(salary) from employees; //query
+-------------+
| sum(salary) |
+-------------+
| 14000 |
+-------------+

17. Count the number of the employees of your company?


mysql> select count(id) from employees; //query
+-----------+
| count(id) |
+-----------+
| 4|
+-----------+

17. Count the number of the employees by city wise?


mysql> select count(id),name,city from employees group by city; //query
+-----------+---------+----------+--------+----------+
| count(id) | name | city |
+-----------+---------+----------+---------+----------+
| 2 | pradeep | varanasi |
| 1 | vivek | ayodhya |
| 1 | pravesh | azamgarh |
+-----------+---------+----------+----------+----------+
18. Count the number of cities and sort the count in ascending order ?
mysql> select count(id),name,city from employees group by city order by count(id) asc;
//query
+-----------+---------+----------+---------+----------+
| count(id) | name | city |
+-----------+---------+----------+----------+----------+
| 1 | vivek | ayodhya |
| 1 | pravesh | azamgarh |
| 2 | pradeep | varanasi |
+-----------+---------+----------+----------+-----------+
19. Group the employees based on their salary ?
mysql> select count(salary) from employees group by salary; //query
+---------------+
| count(salary) |
+---------------+
| 1|
| 1|
| 1|
| 1|
+---------------+
20. Group the employees based on their city?
mysql> select count(city) from employees group by city; //query
+-------------+
| count(city) |
+-------------+
| 2|
| 1|
| 1|
+-------------+

21. Group the city name group by "varanasi"?


mysql> select count(city) from employees group by city="varanasi"; //query
+-------------+
| count(city) |
+-------------+
| 2|
| 2|
+-------------+

22.Convert all the employees names in Upper case?


mysql> select ucase(name) from employees; //query
+-------------+
| ucase(name) |
+-------------+
| PRADEEP |
| VIVEK |
| PRAVESH |
| SANJAY |
+-------------+

23.Convert all the employees names in Lower case?


mysql> select lcase(name) from employees; //query
+-------------+
| lcase(name) |
+-------------+
| pradeep |
| vivek |
| pravesh |
| sanjay |
+-------------+
Lecture Title: SQL Query Lecture 2

Q1. Give me the top two records from the employees table?
mysql> select * from employees order by id asc limit 2; //query
+------+---------+---------+--------+
| id | name | city | salary |
+------+---------+---------+--------+
| 1 | pradeep | varansi | 2000 |
| 2 | vivek | ayodhya | 3000 |
+------+---------+---------+--------+

Q2. Give me the first record from the employees table?


mysql> select * from employees order by id asc limit 1; //query
+------+---------+---------+--------+
| id | name | city | salary |
+------+---------+---------+--------+
| 1 | pradeep | varansi | 2000 |
+------+---------+---------+--------+

Q3. Give me the last two records from the employees table?
mysql> select * from employees order by id desc limit 2; //query
+------+---------+----------+--------+
| id | name | city | salary |
+------+---------+----------+--------+
| 4 | sanjay | varansi | 5000 |
| 3 | pravesh | azamgarh | 4000 |
+------+---------+----------+--------+
Q4. Give me the first two letters of the city from the employees table?
mysql> select mid(city,1,3) from employees; //query
+---------------+
| mid(city,1,3) |
+---------------+
| var |
| ayo |
| aza |
| var |
+---------------+
Q5. Give me the current system time using SQL ?
mysql> select now(); //query
+---------------------+
| now() |
+---------------------+
| 2022-09-29 01:40:13 |
+---------------------+

Q6. Change the current system timeEStamp using SQL ?


mysql> select now() as time; //query
+---------------------+
| time |
+---------------------+
| 2022-09-29 01:43:35 |
+---------------------+

Q7. In the Output change the column name and print the output ?
mysql> select name as Ename from employees; //query
+---------+
| Ename |
+---------+
| pradeep |
| vivek |
| pravesh |
| sanjay |
+---------+

WILDCARD
Q8. Give me the name of the employees which name ending with letter k?
mysql> select name from employees where name like "%k"; //query

+-------+
| name |
+-------+
| vivek |
+-------+
Q9. Give me the name of the employees which name consists of letter e?
mysql> select name from employees where name like "%e%"; //query
+---------+
| name |
+---------+
| pradeep |
| vivek |
| pravesh |
+---------+

Q10. Give me the name of the employees with the start letter p?
mysql> select name from employees where name like "p%"; //query
+---------+
| name |
+---------+
| pradeep |
| pravesh |
+---------+

Q11. Give me the name of the employees which consists of five(5) letters ?
mysql> select name from employees where name like "_ _ _ _ _"; //query
+-------+
| name |
+-------+
| vivek |
+-------+

Q12. Give me the name of the employees which consists of five(5) letters including v ?
26:00 for ans

Q13. Delete a record from the employees table where employees id 101 ?
mysql> delete from employees where id =101; //query
Query OK, 0 rows affected

Q14. Delete a record from the employees table where employees id 101 and 1 ?
mysql> delete from employees where id in(101,1); //query
Query OK, 0 rows affected
Q14. Delete a record from the employees table where employee name is pradeep?
mysql> delete from employees where name = 'pradeep'; //query
Query OK, 0 rows affected

How to get a distinct(unique) record from a table

Q14. Print only Unique city name from the table?


mysql> select distinct city from employees; //query
+----------+
| city |
+----------+
| ayodhya |
| azamgarh |
| varanasi |
+----------+
Q15. Print only Distinct salary name from the table?
mysql> select distinct salary from employees; //query
+--------+
| salary |
+--------+
| 3000 |
| 4000 |
| 5000 |
+--------+
Q16. Concat employee name and salary and then print output?
mysql> select concat (name,salary) from employees; //query
+----------------------+
| concat (name,salary) |
+----------------------+
| vivek3000 |
| pravesh4000 |
| sanjay5000 |
+----------------------+

Q17. Separate the employee name and salary with an underscore then concat the content
of these columns?
mysql> select concat (name,"_",salary) from employees; //query
+--------------------------+
| concat (name,"_",salary) |
+--------------------------+
| vivek_3000 |
| pravesh_4000 |
Q18. Remove white space from the left side of the employee name and then print name?
mysql> select ltrim(name) from employees; //query
+-------------+
| ltrim(name) |
+-------------+
| vivek |
| pravesh |
| sanjay |
+-------------+

Q19. Remove white space from the right side of the employee name and then print name?
mysql> select rtrim(name) from employees; //query
+-------------+
| rtrim(name) |
+-------------+
| vivek |
| pravesh |
| sanjay |
+-------------+
Q20. Remove white spaces from the right side and the left side of the employee name and
then print name?
mysql> select trim(name) from employees; //query
+------------+
| trim(name) |
+------------+
| vivek |
| pravesh |
| sanjay |
+------------+
Q21. Remove white spaces from the name and salary from both left and right side then
concat them separated with underscore?
mysql> select concat(trim(name),"_",trim(salary)) from employees; //query
+-------------------------------------+
| concat(trim(name),"_",trim(salary)) |
+-------------------------------------+
| vivek_3000 |
| pravesh_4000 |
| sanjay_5000 |
+-------------------------------------+
Q22. concat name and salary and print the output in descending order ?
mysql> select concat(name,salary) from employees order by concat(name,salary) desc;
//query

+---------------------+
| concat(name,salary) |
+---------------------+
| vivek3000 |
| sanjay5000 |
| pravesh4000 |
+---------------------+

Q23. Sort the employee name in ascending order ?


mysql> select name from employees order by name asc; //query
+---------+
| name |
+---------+
| pravesh |
| sanjay |
| vivek |
+---------+

Q23. Update the record where the employee name is vivek to abc?
mysql> update employees set name ="abc" where name ="vivek"; //query
Query OK, 1 row affected
Rows matched: 1 Changed: 1

Q24.update the employee name vivek to abc where id is 101?


mysql> update employees set name = "abc" where id =101; //query
Query OK, 0 rows affected
Rows matched: 0 Changed: 0

Q25. Update the employee id from 101 to 110?


mysql> update employees set id=110 where id=101; //query
Query OK, 0 rows affected
Rows matched: 0 Changed: 0

Q26. Replace city name bangalore with bangaluru in the table?


mysql> update employees set city="bangaluru" where city="bangalore"; //query
Query OK, 0 rows affected
Rows matched: 0 Changed: 0

Q27. Delete a record from the table wherever you find vivek?
mysql> delete from employees where name ="vivek"; //query
Query OK, 0 rows affected (0.01 sec)

Q28. Delete a record whosoever salary is 12000?


mysql> delete from employees where salary =12000; //query
Query OK, 0 rows affected

Q29. Update everyone’s salary by 200 ?


mysql> update employees set salary =salary+200; //query
Query OK, 3 rows affected
Rows matched: 3 Changed: 3

Q30.Decrease the salary of every employee by 500 ?


mysql> update employees set salary=salary-500; //query
Query OK, 3 rows affected (0.11 sec)
Rows matched: 3 Changed: 3

Q31. Add a column to the existing table?


mysql> alter table employees add email varchar(128); //query
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0

Q32. Add a column in the beginning of the table?


mysql> alter table employees add age int first; //query
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

Q33. Add a column mobile after the email column of the table?
mysql> alter table employees add mobile int(10) after email; //query
Query OK, 0 rows affected, 1 warning (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 1

Q34. Delete the column email from the table?


1:07:00 for ans

Q35. Delete the column of the employees where name is name?


1:08:00 for ans
Q36. Find the length of each and every employee name in the table?
mysql> select length(name) from employees; //query
+--------------+
| length(name) |
+--------------+
| 3|
| 7|
| 6|
+--------------+

Q37. Find the length of the String “i will kill you “ in the table?
mysql> select length("i will kill you"); //query
+---------------------------+
| length("i will kill you") |
+---------------------------+
| 15 |
+---------------------------+
Lecture Title: SQL Query Lecture 3

Q1. Give me the second maximum salary?


mysql> select max(salary) from employees where salary<(select max(salary) from
employees); //query
+-------------+
| max(salary) |
+-------------+
| 3700 |
+-------------+

Q2. Give me the employee name who got incentives?


mysql> select name from employees where id in(select id from inceincentive_list); //query
+---------+
| name |
+---------+
| abc |
| pravesh |
| sanjay |
+---------+
Q3. Give me the employee name(unique) who got incentives?
mysql> select distinct name from employees where id in(select id from
inceincentive_list); //query
+---------+
| name |
+---------+
| abc |
| pravesh |
| sanjay |
+---------+
Q4. Give me the employee name who is married?
mysql> select name from employees where id in(select id from status where
marital_status ="yes"); //query
+---------+
| name |
+---------+
| abc |
| pravesh |
| sanjay |
+---------+
Q5. Give me the employee name only who is male and also their salary?
mysql> select name,salary from employees where id in(select id from status where
gender ="M"); //query
+---------+--------+
| name | salary |
+---------+--------+
| abc | 2700 |
| pravesh | 3700 |
| sanjay | 4700 |
+---------+--------+

Q6. Give me the employee name who got the incentive amount >=2700?
mysql> select name from employees where id in(select id from inceincentive_list where
incentive_amount>=2700); //query

45:45 for ans

Q7. Give me the employee name who is married and has got incentives ?
mysql> select name from employees where id in(select id from inceincentive_list where id
in (select id from status where marital_status="yes")); //query
+---------+
| name |
+---------+
| abc |
| sanjay |
| pravesh |
+---------+
Q8. Give me the marital status of employees whose salary >3000 ?
mysql> select marital_status from status where id in(select id from employees where
salary>3000); //query
+----------------+
| marital_status |
+----------------+
| yes |
| yes |
+----------------+
Q9. Give me the marital status of employees whose salary >3000 ?
Lecture Title: SQL Query Lecture 4

Q1. In mysql what is the default date format?


Ans. yyyy/mm/dd
Q2. In mysql what is the default time format?
Ans. HH:mm:ss

Q3. Create database Animal


Ans. mysql> create database Animal; //querry
mysql> use Animal; //querry

Q4. Create table Pet


Ans. mysql> create table Pet(Name varchar(20),Owner varchar(20),Birth_Date
Date,Gender Enum('F','M'),Pid int(5)); //querry

Q5. desc Pet


Ans. mysql> desc Pet;
+------------+---------------+------+-----+---------+-------+-------+--------+---------
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+-------+-------+-----------
| Name | varchar(20) | YES | | NULL | |
| Owner | varchar(20) | YES | | NULL | |
| Birth_Date | date | YES | | NULL | |
| Grnder | enum('F','M') | YES | | NULL | |
| Pid | int | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+---------+--------+--------

Q6. Inserting values


Ans. mysql> insert into pet values('Tiger','Vivek',"2015/01/23",'M',100); //querry

Q7. to know the details of tables


Ans. mysql> select * from pet; //querry
+-------+-------+------------+--------+------+------+-------+-----
| Name | Owner | Birth_Date | Grnder | Pid |
+-------+-------+------------+--------+------+------+-------+-----
| Tiger | Vivek | 2015-01-23 | M | 100 |
+-------+-------+------------+--------+------+-------+--------+-----

Q8. Create table registration


Ans. mysql> create table registration(First_Name Varchar(20),Last_Name
Varchar(20),Dob date,Reg_Time TimeStamp,Location Varchar(20),Rid int(5)); //querry
Q9. desc registration

Ans. mysql> desc registration; //query


+------------+-------------+------+-----+---------+-------+------+-------+-------
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+------+-------+--------
| First_Name varchar(20) | YES | | NULL | |
| Last_Name | varchar(20) | YES | | NULL | |
| Dob | date | YES | | NULL | |
| Reg_Time | timestamp | YES | | NULL | |
| Location | varchar(20) | YES | | NULL | |
| Rid | int | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+-------+-------+------

Q10. To insert value in table


Ans. mysql> insert into registration values('lilly','silly',"2022-12-12",now(),'bangalore',100);
//query

Q11. To see value in table


Ans. mysql> select * from registration; //query
+------------+-----------+------------+---------------------+-----------+------+-------+-----------+----------+--------+
| First_Name | Last_Name | Dob | Reg_Time | Location | Rid |
+------------+-----------+------------+---------------------+-----------+------+--------+---------+----------+----------
| lilly | silly | 2022-12-12 | 2022-10-02 02:03:08 | bangalore | 100 |
+------------+-----------+------------+---------------------+-----------+------+--------+--------+----------+---------+----
Lecture Title: SQL Query Lecture 5

Constraints - constraints are nothing but restrictions .Constraints are used to limit the type of
data that can go into a table.

Not null - If you make a column not null then it means it can consist of duplicate values but not
null.

Unique - It can consist of only unique records and also null values as two null values can never be
the same.

Primary Key - It is unique and not null.

Enum - Enum gives us a fixed value to be selected.

Set- set is a group of values and this constraints help us select any one values from the set or all
values from the set or few values from the set.

Lecture Title: SQL Query Lecture 6

Creating a table with the help of constraints.


Create table student

Column name Values

SID int(10) 100,


Name varchar(45) ’xyz’,
Gender Enum(‘F’,’M’) ‘F’
Dob Date “1999-02-23”
Phone number(10) varchar unique ‘9876543210’
Location varchar “bangalore”
Certificate SET( ‘QTP’,’Selenium’,’j2ee’) ‘QTP,Selenium’

Lecture Title: SQL Query Lecture 7

Foreign Key - Foreign key helps us build the relation between the two tables.
Foreign keys can consist of repeated values.

mysql> create table employee( EMPID int AUTO_INCREMENT, ENAME varchar(20) not null, Dept
varchar(20) not null, City varchar(20) not null, primary key(EMPID)); //query
mysql> create table ATTENDANCE( EMPID int not null, ENAME varchar(20) , date varchar(20),
foreign key(EMPID) REFERENCES Employee(EMPID)); //query

Lecture Title: SQL Query Lecture 8

Subtracting two tables in MySql:

Select * from Student where SID not in(Select SID from Attendance); //query

Select * from Student where SID not in(Select unique SID from Attendance); //query

Q1. What is join operation in mysql?


Ans . If you want to display column belongs to different tables then we use join operation.

Inner join - It gives matching records from two tables. And the output is now consisting
column belongs that two tables.
Example:
mysql> select employee.ENAME, employee.DEPT, attendance.date from employee inner
join attendance on employee.EMPID=attendance.EMPID; //query
+--------+---------+------+-----+---------+
| ENAME | DEPT | date |
+--------+---------+------+-----+--------+
| Smith | Science | 1 |
| Carl | Social | 3 |
| Jimmy | Maths | 4 |
| Vivek | Java |5 |
| Sanjay | Coding | 9 |
| Sanjay | Coding | 9 |
+--------+---------+------+-------+--------+

Lecture Title: SQL Query Lecture 9

You might also like