SQL Notes
SQL Notes
Database changed
3.To show tables in database -
mysql> show tables; //query
+-----------------------+
| Tables_in_crud_demo_1 |
+-----------------------+
employees
student
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 |
+---------+
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 |
+------+---------+---------+--------+
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 |
+---------------------+
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
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. 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
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)
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
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
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
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
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.
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.
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
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
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 |
+--------+---------+------+-------+--------+