Saturday, May 20, 2017

Arithmatic Expression in MySQL-Addition,subtraction Multiplication and modulo operator

Arithmatic Expression in MySQL

Arithmatic Expression are the combination of arithmatic operator,constant numeric value and column name  in the SQL statements.
A list of arithmatic opertors are given below -
1.Addition(+)
2.Subtraction(-)
3.Multiplication(*)
4.Division(/)
5.Remainder or Modulo operator(%)

Note-
1.You can use arithmatic operator in any clause in SQL statement except FROM cluase.
2.The operator priority list is given below-
  * / + and -
 Priority Rule -Multiplication and division take the priority over Addition and Division.operators of same priority can be evaluated from left to right.
Parentheses are used to define the priority explicitly to remove the ambiguity.

Example: SQL Arithmetic Operators
1.SELECT 150+10-5*5/5;

Result:
Number of Records: 1
150+10-5*5/5
155

=======

Suppose we have the empsal table with five record and its details are given below-
Create table empsal ( id nuber , name varchar2(30),salary number(8,2));
insert into empsal(id,name, salary) values(1,'Ajay', 10000);
insert into empsal (id,name, salary) values(2,'Amit', 11000);
insert into empsal (id,name, salary) values(3,'Ashok', 7000);
insert into empsal (id,name, salary) values(4,'Aarif', 6000);
insert into empsal (id,name, salary) values(5,'Vinod', 5000);
2. verify the record
Result:
Number of Records: 5
id name salary
1 Ajay 10000
2 Amit 11000
3 Ashok 7000
4 Aarif 6000
5 Vinod 5000

SQL plus (+) operator:It is used to add two or more numbers/expressions
Example-
1.For Number computation
SELECT 150+10;

2.Write a query to find the the increment of the salary of employee by Rs 300.
SELECT Id, name , salary+300 as " Salary" from empsal;
Result:
Number of Records: 5
id name Salary
1 Ajay 10300
2 Amit 11300
3 Ashok 7300
4 Aarif 6300
5 Vinod 5300

2.SQL minus (-) operator -subtract one expression /number from another expression /number.

Example-
1.select 150-5;

Number of Records: 1
150-5
145

2.Write a query to display the Ashok monthly salary after PF deduction of Rs 1500.
 SELECT name, salary-1500 "Ashok In hand Salary" FROM empsal where id=3;

Result:
Number of Records: 1
name Ashok In hand Salary
Ashok 5500

3.SQL multiply ( * ) operator  used to multiply two or more expressions or numbers.

Example-

1.Display the Ashok's Annual salary.
SELECT Id, name , salary as "Monthly Salary", 12*salary as "Yearly Salary" from empsal

Result:
Number of Records: 1
id name Monthly Salary Yearly Salary
3 Ashok 7000         84000

4.SQL divide ( / ) operator-divide one expressions /numbers by another.
1.Write a query to share the Ashok's Annual salary among his four friends equally.
SELECT Id, name ,(12*salary)/4 as "Ashok's friend share" from empsal where id=3

Result:
Number of Records: 1
id name Ashok's friend share
3 Ashok 21000

Wednesday, March 15, 2017

Sorting ASC,DESC-ORDER BY clause display the rows in ASC|DESC order for an expression,column position or an alias.

ORDER BY Clause-
The ORDER BY clause is used to sort the data in ascending(its default ) or 
descending order of one or more columns and use the key word ORDER BY.
ORDER BY clause display the rows in ASC|DESC order for an expression,column position or an alias. 

Syntax-

SELECT   *|[DISTINCT]Column|expression [Alias]
From Table_Name
[WHERE <Condition(s)>]
[ORDER BY Column|Expression [ASC|DESC]];

SELECT -a list of columns
* -Select the all columns 
DISTINCT-suppresses the duplicates 
alias-alternate name of the colunm(s) that is/are used as the heading of that column(s).  
Table_name-specifies the table name containing the column(s). 
WHERE<Condition(s)>- the condition must meet to retrieve the data from the database.
ORDER BY Column|Expression -order the rows in Acending or descending order 

Example- 

1.Write the query to find employee information order by name.

Query-    SELECT * FROM employee order by name;
Output-
Number of Records: 5
ID Name email          address phone_number salary
2 Ajay ajay@gmail.com Hyderbad 16756890 51000
4 Aman Aman@gmail.com Lucknow         26756890 51000
3 Amit amit@gmail.com Delhi         16756890 51000
5 Hari Hari@gmail.com Lucknow         36756890 15000
1 sharat sarat@gmail.com Hyderbad 123456890 5000

2.Write the query to selects all employees from the "employee" table, sorted by the "Name" and the "Salary" column.

Query- SELECT * FROM employee order by name, salary;
Output-
Number of Records: 5
ID Name email          address phone_number salary
2 Ajay ajay@gmail.com Hyderbad 16756890 51000
4 Aman Aman@gmail.com Lucknow         26756890 51000
3 Amit amit@gmail.com Delhi         16756890 51000
5 Hari Hari@gmail.com Lucknow          36756890 15000
1 sharat sarat@gmail.com Hyderbad 123456890 5000

3.Display the name and the salary of the employees from the "employee" table, sorted ascending by the name and descending by the salary. 

Query- SELECT name,salary FROM employee order by name, salary DESC;
Output-
Number of Records: 5
Name salary
Ajay 51000
Aman 51000
Amit 51000
Hari 15000
sharat 5000

4.Display the name and the yearly salary of the employees from the employee table and sort by Annual_salary in ascending. 

Query-SELECT Name , salary*12  Annual_salary FROM employee  order by Annual_salary;
 Alternate -SELECT Name , salary*12  Annual_salary FROM employee  order by 2;
Output-
Number of Records: 5
Name Annual_salary
sharat 60000
Hari 180000
Ajay 612000
Amit 612000
Aman 612000

5.Display the name as Employee Name  and the yearly salary  as Annual_salary of the employees from the employee table and 
sorted descending order by the Annual_salary

Query-SELECT Name "Employee Name", salary*12  Annual_salary FROM employee  order by 2 DESC;
Output-
Number of Records: 5
Employee Name Annual_salary
Ajay           612000
Amit           612000
Aman           612000
Hari           180000
sharat           60000

WHERE Clause-To limit the retrieval of the data from the database for given condition

WHERE Clause
Where clause is used to limit the retrieval of the data from the database for given condition 
and use the key word WHERE.Where clause follows the FROM clause in the SQL Statement.

Syntax-

SELECT   *|[DISTINCT]Column|expression [Alias]
From Table_Name;
[WHERE <Condition(s)>]

SELECT -a list of columns
* -Select the all columns 
DISTINCT-suppresses the duplicates 
alias-alternate name of the column(s) that is/are used as the heading of that column(s).  
Table_name-specifies the table name containing the column(s). 
WHERE<Condition(s)>- the condition must meet to retrieve the data from the database.

Note- Condition is combination of column names,constants,variables,expressions and comparison operator.

Example- 

1.Write the query to find the id,name,address of employee whose address is Lucknow.

Query-    select ID,Name,Address from employee where address='Lucknow';
Output-
Number of Records: 2
ID   Name  address
4    Aman  Lucknow
5    Hari  Lucknow

2.Write the query to retrieve employees details whose salary is more than 10000 and less than 50000. 

Query-  SELECT * FROM employee where salary>10000 and salary<50000
Output-
Number of Records: 1
ID  Name      email             address  phone_number   salary
5   Hari   Hari@gmail.com        Lucknow   36756890      15000

3.Write the query to display the id,name and the yearly salary of the employees whose annual salaries are less than 200000.

Query-SELECT ID , Name , salary*12  Annual_salary FROM employee where Annual_salary<200000;
Output-
Number of Records: 2
ID Name     Annual_salary
1 sharat    60000
5 Hari      180000

4.Write the query to display the id,name and the yearly salary of the employees and also label the column heading id as Emp_Id,name as Employee Name and salary as 
Yearly Salary Package.

Query-  SELECT ID as Emp_Id, Name as "Employee Name", salary*12 as "Yearly Salary Package" FROM employee;
Output-
Number of Records: 5
Emp_Id Employee Name     Yearly Salary Package
1         sharat         60000
2         Ajay           612000
3         Amit           612000
4         Aman           612000
5         Hari           180000

SELECT Statement-To retrieve the data from the database

SELECT Statement

Select statement is used to retrieve the data from the database.We use the key word SELECT.
Using the Select statement, we can choose the column(s)of the same table or different tables using the join and 
also can select the number of the rows either same or different tables.

Syntax-

SELECT   *|[DISTINCT]Column|expression [Alias]
From Table_Name;

SELECT -a list of columns
* -Select the all columns 
DISTINCT-suppresses the duplicates 
alias-alternate name of the column(s) that is/are used as the heading of that column(s).  
Table_name-specifies the table names containing the column(s). 

Example- 

1.Write the query to select the all columns of the table employee.
Query-    select * from employee;
Output-
Number of Records: 5
ID Name email                 address       phone_number salary
1 sharat sarat@gmail.com          Hyderabad 123456890 5000
2 Ajay ajay@gmail.com          Hyderabad 16756890 51000
3 Amit amit@gmail.com          Delhi         16756890 51000
4 Aman Aman@gmail.com          Lucknow 26756890 51000
5 Hari Hari@gmail.com          Lucknow 36756890 15000

2.Write the query to find the distinct address in the employee table. 
Query-  SELECT distinct address FROM employee
Output-
Number of Records: 3
address
Hyderabad
Delhi
Lucknow

3.Write the query to display the id,name and the yearly salary of the employees and also 
label the column heading id as Employee_Id,name as Name and salary as Annual Salary.
Query-  SELECT ID as Employee_Id, Name as "Name", salary*12 as "Annual salary" FROM employee;
Output-
Number of Records: 5
Employee_Id Name Annual salary
1         sharat 60000
2         Ajay 612000
3         Amit 612000
4         Aman 612000
5         Hari 180000

4.Write the query to display the id,name and the yearly salary of the employees  whose annual salaries are greater 200000 and also 
label the column heading id as Employee_Id,name as Name and salary as Annual_Salary.
Query-SELECT ID as Employee_Id, Name as "Name", salary*12  Annual_salary FROM employee where Annual_salary>200000;
Output-
Number of Records: 3
Employee_Id Name Annual_salary
2        Ajay 612000
3        Amit 612000
4        Aman 612000

Sunday, February 19, 2017

Developing the mySQL interview skills- For beginner to expert

Saturday, February 18, 2017

Introduction to the database -Mysql

Data is the fact  and it can be recorded and stored.
A collection of related data is called the database.
There are different types of database such as RDBMS,ORDBMS,OODBMS etc.
Mysql is an example of open source relational database