In my previous articles i have given the different SQL Queries examples- SQL for Testers, SQL for Business Analysts and so on. In this article i would like to give the idea about SQL queries for Data analysis.What do you mean by data analysis? -Data analysis is nothing but fetching the data for analysis purpose and we can utilize that data for taking business decisions.
SQL Queries for Data Analysis Examples :
In this section i will give you multiple examples of SQL queries which are mostly used for data analysis purpose. I will start with simple data analysis examples and will cover the complex examples as well. The main purpose of SQL queries to use for data analysis is for taking the business details very easily.
Simple Queries for data analysis :
- Fetch the data from table :
The simple select query is very useful to fetch the data from table. I would like to give the example for fetching the data from Table.
Example : If user wants to fetch and analyse the first 1000 records from employee table.
Select * from Employee where rownum <=1000;
The above query is used to fetch first 1000 records from Employee table.
PostgreSQL Solution :
Select * from Employee limit 1000;
2.Fetch the data where Profession is Engineer.
Select * from Employee where profession=’Engineer’;
The above query will fetch the data from employee table whose profession is ‘Engineer’.
3.Fetch the data where profession is Engineer or Doctor.
Select * from Employee where profession IN (‘Engineer’,’Doctor’);
4.Fetch the data from Employee table where Salary of employee is between 10000 to 30000.
Select E.*, from Employee E,Salary S where E.Emp_id=S.Emp_id and s.salary between 10000 and 30000;
5.Fetch the Employee name and Salary from the Employee and salary table.
Select E.Employee_name and S.Salary from Employee E,Salary S where E.Emp_id=S.Emp_id;
6.Fetch the distinct country name from Employee table.
Select distinct Country from Employee;
7.If you want to fetch the data for Employees whose country is Finland and city is Helsinki. (Example with AND Operator)
Select * from Employees where Country_name=’Finland’ and City_name=’Helsinki’.
8.Fetch the data for Employee where if Country is India then Salary in Rupees and Country is Finland Salary in Euros.
Example for Case When Statement
Select *,Case when Country=’India’ Then ‘Rupee’
when Country=’Europe’ Then ‘Euro’
Else ‘Dollars’ As “Currency”
9.Fetch the number of employees who are from Finland.
Select count(*) from Employee where country=’Finland’;
10.Fetch the number of employees countrywise.
Select Country_name, count(*) from Employee group by Country_name;
11.Fetch number of Employees countrywise where count of employee more than 1000.
Select Country_name, count(*) from Employee group by Country_name having count(*)>1000;
12.Fetch the Employees who are not assigned to any department.
Select * from Employees where department_name is null;
13.Fetch the employees whose country is Either ‘India’ or ‘USA’.
Select * from Employees where country =’India’ or Country=’USA’;
14.Fetch the Employees salariwise in descending order.
Select * from Employees order by salary desc;
These are some most important queries which are using for data analysis and management. I hope you will like this article on SQL Queries for Data analysis and hope it is helpful. If you have any questions related to SQL Queries for Data analysis kindly comment in to comments section.