SlideShare a Scribd company logo
1 of 11
Download to read offline
Part 1 of 2 1
By Manjeet Singh Bargoti
DATABASE (Part -1)
Database is piece of software which help us to store data permanently inside the table in
organised manner. Some of databeses is available MySQL, DB2, Oracle, HSQL etc.
How to Setup:
 Download XAMPP or WAMP or MAMP and install in your Computer or Laptop.
 Launch Control panel and start MySQL and Apache.
Connecting with MySQL Database:
 Open Command prompt in window & change the path to bin folder of MySQL as
shown below.
C:/user/HACK > cd C:/xampp/MySQL/bin
 Go to control panel of XAMPP (in my computer I’m using XAMPP Server) and
Start MySQL database.
C:/xampp/MySQL/bin > mysql –u root
 Maria DB [(none)] > show databases;
Database
information scheme
mysql
test
new scheme
 Maria DB [(none)] > use information scheme[Database name];
Database Name: information scheme
Tables
customer
employee
emp
dept
 Maria DB [information scheme] > select * from customer[Table Name];
Table Name: customer
CID CNAME Age Location
101 Manjeet 22 Bangalore
102 Ankur 24 Bangalore
103 Sajal 25 Gorakhpur
104 Devendra 26 Gurgaon
105 Aditya 28 Delhi
Part 1 of 2 2
By Manjeet Singh Bargoti
Some important Queries of MySQL:
1. Show the all content (data) of table
Select * from customer (table name);
2. Read only customer name and age from customer table.
Select CNAME, Age from customer;
3. Show all customers name who are staying in Bangalore.
Select CNAME from customer
Where location = ‘bangalore’;
4. Show the all customers name and id whose age is less and equal to 25.
Select CNAME, CID from customer
Where age <= 25;
5. Show all employee name which names end with letter ‘a’.
Select CNAME from customer
Where CNAME like “%a”;
6. Show the name of employee from table whose age is 24 and 25.
Select CNAME form customer
Where age in (25, 24);
7. Show me the name of employee from Bangalore and delhi.
Select CNAME from customer
Where location in (‘bangalore’, ‘delhi’);
8. Show the employee name which starts with letter ‘m’.
Select CNAME from customer
Where CNAME like “M%”;
9. Show the employee name which consist the letter ‘a’.
Select CNAME from customer
Where CNAME like “%a%”;
10. Show the employee name which consist only 5 letters.
Select CNAME from customer
Where CNAME like “_ _ _ _ _”;
11. Show the employee name which start with letter ‘a’ and consist only 5 letters.
Select CNAME from customer
Where CNAME like “a_ _ _ _”;
Table Name: employee
EID ENAME Salary Dept
101 Shivesh 10000 Science
102 Praveen 50000 Math
103 Ankur 35000 Science
104 Devendra 25000 Science
105 Aditya 28000 Social
Part 1 of 2 3
By Manjeet Singh Bargoti
12. Show the first maximum salary from the table.
Select max (salary)
From employee;
13. Show the total salary which I am paying to employee.
Select sum (salary)
From employee;
14. Show the minimum salary from the table.
Select min (salary)
From employee;
15. Show me the average salary which I am paying.
Select avg (salary)
From employee;
16. Show me the number of employee in employee table.
Select count (EID)
From employee;
17. Print the employee name in upper case.
Select ucase (ENAME)
From employee;
18. Print the employee name in lowercase.
Select lcase (ENAME)
From employee;
19. Remove white space from left side of the string.
Select ltrim (ENAME)
From employee;
20. Remove white space from right side of the string.
Select rtrim (ENAME)
From employee;
21. Remove the white space from both side of the string.
Select trim (ENAME)
From employee;
22. In the output give me only first three letters on department name.
Select mid (dept, 1, 3)
From employee;
23. Show me the current date and time.
Select now ()
From employee;
24. Show me the data of table in descending order.
Select * from employee
Order by EID Desc;
25. In table employee short the data in ascending order.
Select * from employee
Order by EID ASC;
26. Show me top two records from employee table.
Select * from employee
Order by EID ASC limit 2;
Part 1 of 2 4
By Manjeet Singh Bargoti
27. Show me the last two records from the employee table.
Select * from employee
Order by EID Desc limit 2;
SQL: Structured Query Language
Select
From
Group by
Having
Order by
Creating a Table in myphpadmin:
 Launch XAMPP and open Control panel and start Apache.
 Now launch your installed browser and in the browser type localhost.
 Click on PhpMyAdmin.
 Click the database name or create a new database by click on new.
 Click on create.
 Create a table in DB by giving the table name and specify the no. of column and
click on go.
 Give column name and select the right datatype.
 To insert the data click on insert.
Table Name: customer
CID CNAME Age Location
101 Manjeet 22 Bangalore
102 Ankur 24 Bangalore
103 Sajal 25 Gorakhpur
104 Devendra 26 Gurgaon
105 Aditya 28 Delhi
28. In the above table update the customer name Sajal to Vishal.
Update customer
Set Name = ‘Vishal’ Where CID = 103;
29. Delete a particular row from table.
Delete from customer
Where CID = 102;
30. Delete the customer records having ID 102, 103.
Delete from customer
Where CID in (102, 103);
31. Update customer record to Vishal for all those name starts with letter ‘s’.
Update customer
Set name = “Vishal” Where CNAME like “s%”;
Part 1 of 2 5
By Manjeet Singh Bargoti
Sub-Query: A query within a query is called subquery. Always inner query will execute
first and output of inner query will be given to the outer query as input.
Rules to build SubQuery:
 There should be common column in the table.
 Manually analysis which one is outer and which one is inner.
 Output should consist of average one column.
Table Name: emp++
EID ENAME Salary Location
101 X 10000 Bangalore
102 Y 25000 Chennai
103 Z 15000 GOA
104 A 36000 Hyderabad
32. Second maximum salary from the table.
Select max (salary) from emp
Where salary < 36000;
Alternate method:
Select max (salary) from emp
Where salary < (Select max (salary) from emp);
33. Third max salary from the table.
Select max (salary) from emp
Where salary < (Select max (salary) from emp
Where salary < (Select max (salary) from emp));
Table Name: emp Table Name: dept
EID ENAME Salary Location EID Dept
101 X 10000 Bangalore 101 Science
102 Y 25000 Chennai 101 Maths
103 Z 15000 GOA 102 Social
104 A 36000 Hyderabad 103 Science
34. Give me the employee name working in science department.
Select ENAME from emp
Where EID in (Select EID from dept where dept = “Science”);
35. Give me employee name and salary working for science and maths dept.
Select ENAME, Salary from emp
Where EID in (Select EID from dept where dept in (“Science”, “Maths”));
36. Give me the dept name of the employee whose salary more than 15000.
Select dept from dept
Where EID in (Select EID from emp where salary > 15000);
Part 1 of 2 6
By Manjeet Singh Bargoti
Table Name: employee
EID ENAME LNAME Salary Joining_Date Dept Location
1 Johan A 10000 01 Jan, 2013 Banking Bangalore
2 Michal C 18000 01 Jan, 2013 Insurance Delhi
3 Ray T 25000 01 Feb, 2013 Insurance Mumbai
4 Tom J 36000 01 Feb, 2013 Insurance Bangalore
5 Jarry P 19000 01 Feb, 2013 Insurance Chennai
6 Phillip P 9000 01 Jan, 2013 Science Bangalore
7 Mike K 23000 01 Jan, 2013 Science Mumbai
8 Smith K 20000 01 Feb, 2013 Insurance Delhi
Table Name: incentive
EID IncentiveDate IncentiveAmount
1 01 Feb, 2013 5000
2 01 Feb, 2013 3000
3 01 Feb, 2013 4000
1 01 Jan, 2013 4500
2 01 Jan, 2013 3500
37. Get First Name & Last Name from employee table.
Select ENAME, LNAME from employee;
38. Get First Name from employee.
Select ENAME from employee;
39. Give me unique (distinct) department from employee name.
Select DISTINCT dept from employee;
40. Give me distinct (unique) incentive date.
Select DISTNICT incentivedate from incentive;
41. Give me the length of every name present in the first name column in
employee table.
Select length (ENAME) from employee;
42. Give me the first name of the employee table by replacing ‘o’ with ‘$’.
Select replace (ENAME, ‘o’, ‘$’) from employee;
43. Give me the first name and last name in a single column separated by
underscore.
Select concat (ENAME, ‘_’, LNAME) from employee;
44. Get the first name in ascending order.
Select FNAME from employee
Order by ENAME AES;
45. Get employee detail from employee name whose salary between 15000 to
30000.
Select * from employee
Where salary
Between 15000 and 30000;
Part 1 of 2 7
By Manjeet Singh Bargoti
46. Give me the first name of employee who’s joining date between 01 Jan, 2013
to 01 March, 2013.
Select * from employee
Where joining_date
Between 01 Jan, 2013 and 01 March, 2013;
47. Give me the first name of employee whose incentive amount greater than
equal to 4500.
Select ENAME from employee
Where EID in (Select EID from incentive
Where incentiveAmount >=4500);
48. Show me the First name of employee who is getting incentive.
Select ENAME from employee
Where EID in (Select EID from incentive);
49. Show me the First name of employee who did not get incentive.
Select ENAME from employee
Where EID in (Select EID from incentive);
50. Count the number of people city wise and give the o/p.
Select count (CID) from employee
Group by location;
51. Give me the count of people in Bangalore city.
Select count (CID) from employee
Group by location
Having location = “Bangalore”;
52. Give me the incentive amount only of the employee john.
Select incentiveAmount from incentive
Where EID = (Select EID from employee
Where ENAME = “john”);
53. Give top 2 salaries from the employee table.
Select salary from employee
Order by salary desc
Limit 2;
54. Write a query for drop table employee.
Drop table employee;
Joins: When in the output you require column name or names that belongs to different
tables then we use joins.
Rules of Join:
 Column name should belong to the different table in order to print the output.
 There should be common column between these two tables so that we compare
the data of table.
Inner Join: Gives matching records from both the tables.
Right Join: Right join return all records from right table with matching records from left
table.
Part 1 of 2 8
By Manjeet Singh Bargoti
Table Name: employee Table Name: incentive
EID ENAME Salary dept EID Incentivedate Incentiveamount
101 XYZ 10000 Science 101 01 Feb, 2013 10000
102 AAA 10000 Maths 104 01 Jan, 2013 10000
103 XXX 15000 Social 102 01 March, 2013 15000
55. Give me employee name and incentive date of the employee who has got
incentive.
Select employee.ENAME, incetive.Incentivedate from employee
Inner join incentive on
employee.EID = incentive.EID;
56. Give me the employee name and salary and incentive amount of those
employees who has receive incentive.
Select employee.ENAME, employee.Salary, incentive.Incentiveamount
From employee inner join incentive on
Employee.EID = incentive.EID;
57. Give me the employee name who got incentive on 1st March, 2013.
Select ENAME from employee
Where EID in (Select EID from incentive
Where incentivedate = “01 March. 2013”);
58. Give me the employee ID and incentive amount.
Select EID, incentiveamount from incentive;
59. Show me the dept name and incentive amount.
Select employee.dept, incentive.incentiveamount
From employee inner join incentive on
Employee.EID = incentive.EID;
60. Give me the incentive date who got the incentive 4500 to 6000.
Select incentivedate from incentive
Where incentiveamount between 4500 and 6000;
61. Give all the records from left table with matching records from right table.
Select * from employee
Left join incentive on
Employee.EID = incentive.EID;
62. Give me employee name and incentive amount for permanently work in
company.
Select employee.ENAME, incentive.incentiveamount
From employee left join incentive on
Employee.EID = incentive.EID;
63. Give me the employee name and salary and incentive amount of permanent
employee.
Select employee.ENAME, employee.Salary, incentive.incentiveamount
Form employee left join incentive on
Employee.EID = incentive.EID;
Part 1 of 2 9
By Manjeet Singh Bargoti
64. Give me the employee name and all records of right table.
Select employee.ENAME, incentive.incentiveamount
From employee right join incentive on
employee.EID = incentive.EID;
Table Name: T1 Table Name: T2 Table Name: T3
A B C A D E A G H I
65. Give me all records from left table T1 only matching records from table T3
and CGH.
Select T1.C, T3.G, T3.H
From T1 left join T3 on
T1.A = T3.A;
66. Give me all the records from T3 and only matching records from table T1 and
output should consist of I & B.
Select T3.I, T1.B
From T3 right join T1 on
T3.A = T1.A;
Primary key: It is unique plus not null. Per table there can be only one primary key.
Unique key: It is unique plus not null value. You can have more than one unique constrain
in the table.
Check: It will validate the data before the storing it, Check the quantity should be greater
than Zero, check salary > 0. You can put more than one constrain in one table.
Not Null: This constrain will not let you to store null value in a particular column. It can
be apply more than one in the table.
There are two ways to decide Primary Key:
 Serogate Primary Key: This is forcefully added by the developer in order to
identify the record in unique.
 Natural Primary key: Many times we create the column. We notice that certain
column will consist unique records in it. Like – phone number column, Email ID
etc.
Foreign Key: The main purpose of foreign key is to create relation between two tables.
In a particular table there can be more than one foreign key. Foreign Key can consist of
duplicate records in it.
Part 1 of 2 10
By Manjeet Singh Bargoti
Steps to create a table using Command Prompt:
 Press Window + R and type cmd in run tab.
 Now you will see a command prompt tab on your computer screen.
 First Login with your database in command prompt (How to login in command
prompt, I already explained in starting of this tutorial).
 Copy the path of MySQL folder present in XAMPP which is in C drive.
 Paste the Path in command prompt and type username root and password blank
or if you give any password.
 Now we will create a table for customer details:
Create table customer (
CID int AUTO_INCREAMENT, Primary Key,
CNAME varchar (20) not null,
LOCATION varchar (20) not null,
AGE int (3),
EMAIL varchar (30) unique,
);
Here varchar(), int() are datatypes.
We will learn in detail about creating database and table using Command Prompt
in Part 2. In Part 2 I will teach you how to insert data in table and how to get data
from table using command prompt.
Important Questions:
Q1. What is SQL Injection?
A. SQL Injection is one of the techniques uses by hackers to hack a website by injecting
SQL commands in data fields.
Q2. Explain the difference between DELETE, TRUNCATE and DROP commands?
A. Once delete operation is performed Commit and Rollback can be performed to retrieve
data. But after truncate statement, Commit and Rollback statement can’t be performed.
Where condition can be used along with delete statement but it can’t be used with
truncate statement. Drop command is used to drop the table or keys like primary, foreign
from a table.
Q3. What is the difference between Cluster and Non cluster Index?
A. A clustered index reorders the way records in the table are physically stored. There
can be only one clustered index per table. It make data retrieval faster. A non-clustered
index does not alter the way it was stored but creates a complete separate object within
the table. As a result insert and update command will be faster.
Part 1 of 2 11
By Manjeet Singh Bargoti
Q4. What is Union, minus and Interact commands?
A. MINUS operator is used to return rows from the first query but not from the second
query. INTERSECT operator is used to return rows returned by both the queries.
Q5. What’s the Difference between a Primary Key and a Unique Key?
A. Both primary key and unique key enforce uniqueness of the column on which they are
defined. But by default, the primary key creates a clustered index on the column, whereas
unique key creates a non-clustered index by default. Another major difference is that
primary key doesn’t allow NULLs, but unique key allows one NULL only.
In the Part 2 we will learn about DDL, DML, Normalization and CRUD
operation using PHP and MySQL.

More Related Content

Similar to Database MySQL (Part-1)

Pooja Bijawat,Bachelor Degree in Computer Application
Pooja Bijawat,Bachelor Degree in Computer ApplicationPooja Bijawat,Bachelor Degree in Computer Application
Pooja Bijawat,Bachelor Degree in Computer Applicationdezyneecole
 
Sql task answers
Sql task answersSql task answers
Sql task answersNawaz Sk
 
Priyanka Bhatia.BCA Final year 2015
Priyanka Bhatia.BCA Final year 2015Priyanka Bhatia.BCA Final year 2015
Priyanka Bhatia.BCA Final year 2015dezyneecole
 
Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015dezyneecole
 
21390228-SQL-Queries.doc
21390228-SQL-Queries.doc21390228-SQL-Queries.doc
21390228-SQL-Queries.docSatishReddy212
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answersMichael Belete
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSgaurav koriya
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankpptnewrforce
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxmetriohanzel
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Achmad Solichin
 

Similar to Database MySQL (Part-1) (20)

Ravi querys 425
Ravi querys  425Ravi querys  425
Ravi querys 425
 
Pooja Bijawat,Bachelor Degree in Computer Application
Pooja Bijawat,Bachelor Degree in Computer ApplicationPooja Bijawat,Bachelor Degree in Computer Application
Pooja Bijawat,Bachelor Degree in Computer Application
 
BIS05 Introduction to SQL
BIS05 Introduction to SQLBIS05 Introduction to SQL
BIS05 Introduction to SQL
 
Sql task answers
Sql task answersSql task answers
Sql task answers
 
Priyanka Bhatia.BCA Final year 2015
Priyanka Bhatia.BCA Final year 2015Priyanka Bhatia.BCA Final year 2015
Priyanka Bhatia.BCA Final year 2015
 
Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015Vishwajeet Sikhwal ,BCA,Final Year 2015
Vishwajeet Sikhwal ,BCA,Final Year 2015
 
Sql server lab_3
Sql server lab_3Sql server lab_3
Sql server lab_3
 
Parent child hier.docx
Parent child hier.docxParent child hier.docx
Parent child hier.docx
 
Sql queries
Sql queriesSql queries
Sql queries
 
21390228-SQL-Queries.doc
21390228-SQL-Queries.doc21390228-SQL-Queries.doc
21390228-SQL-Queries.doc
 
Sql queries questions and answers
Sql queries questions and answersSql queries questions and answers
Sql queries questions and answers
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Sql server lab_4
Sql server lab_4Sql server lab_4
Sql server lab_4
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
 
Les06
Les06Les06
Les06
 
Pooja Jain
Pooja JainPooja Jain
Pooja Jain
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 

Recently uploaded

WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 

Recently uploaded (20)

WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital Businesses
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 

Database MySQL (Part-1)

  • 1. Part 1 of 2 1 By Manjeet Singh Bargoti DATABASE (Part -1) Database is piece of software which help us to store data permanently inside the table in organised manner. Some of databeses is available MySQL, DB2, Oracle, HSQL etc. How to Setup:  Download XAMPP or WAMP or MAMP and install in your Computer or Laptop.  Launch Control panel and start MySQL and Apache. Connecting with MySQL Database:  Open Command prompt in window & change the path to bin folder of MySQL as shown below. C:/user/HACK > cd C:/xampp/MySQL/bin  Go to control panel of XAMPP (in my computer I’m using XAMPP Server) and Start MySQL database. C:/xampp/MySQL/bin > mysql –u root  Maria DB [(none)] > show databases; Database information scheme mysql test new scheme  Maria DB [(none)] > use information scheme[Database name]; Database Name: information scheme Tables customer employee emp dept  Maria DB [information scheme] > select * from customer[Table Name]; Table Name: customer CID CNAME Age Location 101 Manjeet 22 Bangalore 102 Ankur 24 Bangalore 103 Sajal 25 Gorakhpur 104 Devendra 26 Gurgaon 105 Aditya 28 Delhi
  • 2. Part 1 of 2 2 By Manjeet Singh Bargoti Some important Queries of MySQL: 1. Show the all content (data) of table Select * from customer (table name); 2. Read only customer name and age from customer table. Select CNAME, Age from customer; 3. Show all customers name who are staying in Bangalore. Select CNAME from customer Where location = ‘bangalore’; 4. Show the all customers name and id whose age is less and equal to 25. Select CNAME, CID from customer Where age <= 25; 5. Show all employee name which names end with letter ‘a’. Select CNAME from customer Where CNAME like “%a”; 6. Show the name of employee from table whose age is 24 and 25. Select CNAME form customer Where age in (25, 24); 7. Show me the name of employee from Bangalore and delhi. Select CNAME from customer Where location in (‘bangalore’, ‘delhi’); 8. Show the employee name which starts with letter ‘m’. Select CNAME from customer Where CNAME like “M%”; 9. Show the employee name which consist the letter ‘a’. Select CNAME from customer Where CNAME like “%a%”; 10. Show the employee name which consist only 5 letters. Select CNAME from customer Where CNAME like “_ _ _ _ _”; 11. Show the employee name which start with letter ‘a’ and consist only 5 letters. Select CNAME from customer Where CNAME like “a_ _ _ _”; Table Name: employee EID ENAME Salary Dept 101 Shivesh 10000 Science 102 Praveen 50000 Math 103 Ankur 35000 Science 104 Devendra 25000 Science 105 Aditya 28000 Social
  • 3. Part 1 of 2 3 By Manjeet Singh Bargoti 12. Show the first maximum salary from the table. Select max (salary) From employee; 13. Show the total salary which I am paying to employee. Select sum (salary) From employee; 14. Show the minimum salary from the table. Select min (salary) From employee; 15. Show me the average salary which I am paying. Select avg (salary) From employee; 16. Show me the number of employee in employee table. Select count (EID) From employee; 17. Print the employee name in upper case. Select ucase (ENAME) From employee; 18. Print the employee name in lowercase. Select lcase (ENAME) From employee; 19. Remove white space from left side of the string. Select ltrim (ENAME) From employee; 20. Remove white space from right side of the string. Select rtrim (ENAME) From employee; 21. Remove the white space from both side of the string. Select trim (ENAME) From employee; 22. In the output give me only first three letters on department name. Select mid (dept, 1, 3) From employee; 23. Show me the current date and time. Select now () From employee; 24. Show me the data of table in descending order. Select * from employee Order by EID Desc; 25. In table employee short the data in ascending order. Select * from employee Order by EID ASC; 26. Show me top two records from employee table. Select * from employee Order by EID ASC limit 2;
  • 4. Part 1 of 2 4 By Manjeet Singh Bargoti 27. Show me the last two records from the employee table. Select * from employee Order by EID Desc limit 2; SQL: Structured Query Language Select From Group by Having Order by Creating a Table in myphpadmin:  Launch XAMPP and open Control panel and start Apache.  Now launch your installed browser and in the browser type localhost.  Click on PhpMyAdmin.  Click the database name or create a new database by click on new.  Click on create.  Create a table in DB by giving the table name and specify the no. of column and click on go.  Give column name and select the right datatype.  To insert the data click on insert. Table Name: customer CID CNAME Age Location 101 Manjeet 22 Bangalore 102 Ankur 24 Bangalore 103 Sajal 25 Gorakhpur 104 Devendra 26 Gurgaon 105 Aditya 28 Delhi 28. In the above table update the customer name Sajal to Vishal. Update customer Set Name = ‘Vishal’ Where CID = 103; 29. Delete a particular row from table. Delete from customer Where CID = 102; 30. Delete the customer records having ID 102, 103. Delete from customer Where CID in (102, 103); 31. Update customer record to Vishal for all those name starts with letter ‘s’. Update customer Set name = “Vishal” Where CNAME like “s%”;
  • 5. Part 1 of 2 5 By Manjeet Singh Bargoti Sub-Query: A query within a query is called subquery. Always inner query will execute first and output of inner query will be given to the outer query as input. Rules to build SubQuery:  There should be common column in the table.  Manually analysis which one is outer and which one is inner.  Output should consist of average one column. Table Name: emp++ EID ENAME Salary Location 101 X 10000 Bangalore 102 Y 25000 Chennai 103 Z 15000 GOA 104 A 36000 Hyderabad 32. Second maximum salary from the table. Select max (salary) from emp Where salary < 36000; Alternate method: Select max (salary) from emp Where salary < (Select max (salary) from emp); 33. Third max salary from the table. Select max (salary) from emp Where salary < (Select max (salary) from emp Where salary < (Select max (salary) from emp)); Table Name: emp Table Name: dept EID ENAME Salary Location EID Dept 101 X 10000 Bangalore 101 Science 102 Y 25000 Chennai 101 Maths 103 Z 15000 GOA 102 Social 104 A 36000 Hyderabad 103 Science 34. Give me the employee name working in science department. Select ENAME from emp Where EID in (Select EID from dept where dept = “Science”); 35. Give me employee name and salary working for science and maths dept. Select ENAME, Salary from emp Where EID in (Select EID from dept where dept in (“Science”, “Maths”)); 36. Give me the dept name of the employee whose salary more than 15000. Select dept from dept Where EID in (Select EID from emp where salary > 15000);
  • 6. Part 1 of 2 6 By Manjeet Singh Bargoti Table Name: employee EID ENAME LNAME Salary Joining_Date Dept Location 1 Johan A 10000 01 Jan, 2013 Banking Bangalore 2 Michal C 18000 01 Jan, 2013 Insurance Delhi 3 Ray T 25000 01 Feb, 2013 Insurance Mumbai 4 Tom J 36000 01 Feb, 2013 Insurance Bangalore 5 Jarry P 19000 01 Feb, 2013 Insurance Chennai 6 Phillip P 9000 01 Jan, 2013 Science Bangalore 7 Mike K 23000 01 Jan, 2013 Science Mumbai 8 Smith K 20000 01 Feb, 2013 Insurance Delhi Table Name: incentive EID IncentiveDate IncentiveAmount 1 01 Feb, 2013 5000 2 01 Feb, 2013 3000 3 01 Feb, 2013 4000 1 01 Jan, 2013 4500 2 01 Jan, 2013 3500 37. Get First Name & Last Name from employee table. Select ENAME, LNAME from employee; 38. Get First Name from employee. Select ENAME from employee; 39. Give me unique (distinct) department from employee name. Select DISTINCT dept from employee; 40. Give me distinct (unique) incentive date. Select DISTNICT incentivedate from incentive; 41. Give me the length of every name present in the first name column in employee table. Select length (ENAME) from employee; 42. Give me the first name of the employee table by replacing ‘o’ with ‘$’. Select replace (ENAME, ‘o’, ‘$’) from employee; 43. Give me the first name and last name in a single column separated by underscore. Select concat (ENAME, ‘_’, LNAME) from employee; 44. Get the first name in ascending order. Select FNAME from employee Order by ENAME AES; 45. Get employee detail from employee name whose salary between 15000 to 30000. Select * from employee Where salary Between 15000 and 30000;
  • 7. Part 1 of 2 7 By Manjeet Singh Bargoti 46. Give me the first name of employee who’s joining date between 01 Jan, 2013 to 01 March, 2013. Select * from employee Where joining_date Between 01 Jan, 2013 and 01 March, 2013; 47. Give me the first name of employee whose incentive amount greater than equal to 4500. Select ENAME from employee Where EID in (Select EID from incentive Where incentiveAmount >=4500); 48. Show me the First name of employee who is getting incentive. Select ENAME from employee Where EID in (Select EID from incentive); 49. Show me the First name of employee who did not get incentive. Select ENAME from employee Where EID in (Select EID from incentive); 50. Count the number of people city wise and give the o/p. Select count (CID) from employee Group by location; 51. Give me the count of people in Bangalore city. Select count (CID) from employee Group by location Having location = “Bangalore”; 52. Give me the incentive amount only of the employee john. Select incentiveAmount from incentive Where EID = (Select EID from employee Where ENAME = “john”); 53. Give top 2 salaries from the employee table. Select salary from employee Order by salary desc Limit 2; 54. Write a query for drop table employee. Drop table employee; Joins: When in the output you require column name or names that belongs to different tables then we use joins. Rules of Join:  Column name should belong to the different table in order to print the output.  There should be common column between these two tables so that we compare the data of table. Inner Join: Gives matching records from both the tables. Right Join: Right join return all records from right table with matching records from left table.
  • 8. Part 1 of 2 8 By Manjeet Singh Bargoti Table Name: employee Table Name: incentive EID ENAME Salary dept EID Incentivedate Incentiveamount 101 XYZ 10000 Science 101 01 Feb, 2013 10000 102 AAA 10000 Maths 104 01 Jan, 2013 10000 103 XXX 15000 Social 102 01 March, 2013 15000 55. Give me employee name and incentive date of the employee who has got incentive. Select employee.ENAME, incetive.Incentivedate from employee Inner join incentive on employee.EID = incentive.EID; 56. Give me the employee name and salary and incentive amount of those employees who has receive incentive. Select employee.ENAME, employee.Salary, incentive.Incentiveamount From employee inner join incentive on Employee.EID = incentive.EID; 57. Give me the employee name who got incentive on 1st March, 2013. Select ENAME from employee Where EID in (Select EID from incentive Where incentivedate = “01 March. 2013”); 58. Give me the employee ID and incentive amount. Select EID, incentiveamount from incentive; 59. Show me the dept name and incentive amount. Select employee.dept, incentive.incentiveamount From employee inner join incentive on Employee.EID = incentive.EID; 60. Give me the incentive date who got the incentive 4500 to 6000. Select incentivedate from incentive Where incentiveamount between 4500 and 6000; 61. Give all the records from left table with matching records from right table. Select * from employee Left join incentive on Employee.EID = incentive.EID; 62. Give me employee name and incentive amount for permanently work in company. Select employee.ENAME, incentive.incentiveamount From employee left join incentive on Employee.EID = incentive.EID; 63. Give me the employee name and salary and incentive amount of permanent employee. Select employee.ENAME, employee.Salary, incentive.incentiveamount Form employee left join incentive on Employee.EID = incentive.EID;
  • 9. Part 1 of 2 9 By Manjeet Singh Bargoti 64. Give me the employee name and all records of right table. Select employee.ENAME, incentive.incentiveamount From employee right join incentive on employee.EID = incentive.EID; Table Name: T1 Table Name: T2 Table Name: T3 A B C A D E A G H I 65. Give me all records from left table T1 only matching records from table T3 and CGH. Select T1.C, T3.G, T3.H From T1 left join T3 on T1.A = T3.A; 66. Give me all the records from T3 and only matching records from table T1 and output should consist of I & B. Select T3.I, T1.B From T3 right join T1 on T3.A = T1.A; Primary key: It is unique plus not null. Per table there can be only one primary key. Unique key: It is unique plus not null value. You can have more than one unique constrain in the table. Check: It will validate the data before the storing it, Check the quantity should be greater than Zero, check salary > 0. You can put more than one constrain in one table. Not Null: This constrain will not let you to store null value in a particular column. It can be apply more than one in the table. There are two ways to decide Primary Key:  Serogate Primary Key: This is forcefully added by the developer in order to identify the record in unique.  Natural Primary key: Many times we create the column. We notice that certain column will consist unique records in it. Like – phone number column, Email ID etc. Foreign Key: The main purpose of foreign key is to create relation between two tables. In a particular table there can be more than one foreign key. Foreign Key can consist of duplicate records in it.
  • 10. Part 1 of 2 10 By Manjeet Singh Bargoti Steps to create a table using Command Prompt:  Press Window + R and type cmd in run tab.  Now you will see a command prompt tab on your computer screen.  First Login with your database in command prompt (How to login in command prompt, I already explained in starting of this tutorial).  Copy the path of MySQL folder present in XAMPP which is in C drive.  Paste the Path in command prompt and type username root and password blank or if you give any password.  Now we will create a table for customer details: Create table customer ( CID int AUTO_INCREAMENT, Primary Key, CNAME varchar (20) not null, LOCATION varchar (20) not null, AGE int (3), EMAIL varchar (30) unique, ); Here varchar(), int() are datatypes. We will learn in detail about creating database and table using Command Prompt in Part 2. In Part 2 I will teach you how to insert data in table and how to get data from table using command prompt. Important Questions: Q1. What is SQL Injection? A. SQL Injection is one of the techniques uses by hackers to hack a website by injecting SQL commands in data fields. Q2. Explain the difference between DELETE, TRUNCATE and DROP commands? A. Once delete operation is performed Commit and Rollback can be performed to retrieve data. But after truncate statement, Commit and Rollback statement can’t be performed. Where condition can be used along with delete statement but it can’t be used with truncate statement. Drop command is used to drop the table or keys like primary, foreign from a table. Q3. What is the difference between Cluster and Non cluster Index? A. A clustered index reorders the way records in the table are physically stored. There can be only one clustered index per table. It make data retrieval faster. A non-clustered index does not alter the way it was stored but creates a complete separate object within the table. As a result insert and update command will be faster.
  • 11. Part 1 of 2 11 By Manjeet Singh Bargoti Q4. What is Union, minus and Interact commands? A. MINUS operator is used to return rows from the first query but not from the second query. INTERSECT operator is used to return rows returned by both the queries. Q5. What’s the Difference between a Primary Key and a Unique Key? A. Both primary key and unique key enforce uniqueness of the column on which they are defined. But by default, the primary key creates a clustered index on the column, whereas unique key creates a non-clustered index by default. Another major difference is that primary key doesn’t allow NULLs, but unique key allows one NULL only. In the Part 2 we will learn about DDL, DML, Normalization and CRUD operation using PHP and MySQL.