Sql queries
Sql queries
select U.user_id as userId, U.username as userName, P.no_of_post as topPost from Users U join
Posts P ON U.user_id = P.user_id order by P.no_of_post DESC limit 4;
FROM TBLEMPLOYEE E
JOIN TBLEMPLOYEE M ON
E.MANAGERID = M.EMPID
D.DEPARTMENT_NAME,A.FIRST_NAME,A.LAST_NAME,A.SALARY
(SELECT E.FIRST_NAME,E.LAST_NAME,E.DEPARTMENT_ID,S.SALARY
--Course Fees
use DB_Institute;
SELECT E.STUDENTID AS StudentID, E.STUDENTNM AS Name, E.DTENROLL AS
TBL_STUDENTS_ENROLL E JOIN
TBL_STUDENTS_FEES F ON
E.STUDENTID=F.STUDENTID
use Customerdb;
FROM ORDERS
GROUP BY CUSTOMER_ID
)B
EmployeeDB;
-- Environment Specification and Instruction(Top 3 employees with the highest Salary) use
EmployeeDB;
select * from Employees order by salary desc limit 3;
-- Environment Specification and Instruction(3rd Highest Salary from the table) use
DB_Company;
select salary as Salary from TBL_EMPLOYEE order by salary desc limit 1 offset 2;
CustomerDB;
-------------------------------------------------------------------------------------------------
--problem statement --- Write the query that combines the data from the students and teacher's
table (SchoolDB Database Name) SELECT first_name, last_name, 'Student' AS role
FROM students UNION ALL
FROM teachers
ORDER BY first_name;
--problem statement --- Write a sql query to fetch class which has 3 or more students assigned to
it.
SELECT Class FROM Courses
FROM TBL_Employee e
GROUP BY d.Department
ORDER BY d.Department;
-- (Sample output is Biology and Maths) Retrive the most repeated Class from the student table
SELECT Class, COUNT(*) AS NumberOfStudents FROM Student
--Sample output(emily davis , jane smith , john doe , mike johnson , sarah williams)
SELECT e.name AS employee_name FROM Employee e
--problem statement --- Write a sql query to execute the username (part before @) and domain
(part after the '@' symbol)
SELECT
FROM Email_Data
--problem statement--- Write an SQL query to find the most expensive products in each product
category
SELECT category,product_name AS most_expensive_product,price AS max_price FROM products
WHERE(category,price) IN(SELECT category,MAX(price)
__________________________________________________________________________________
__ ___________
--problem statement --- write a query to categorize employees into three groups based on their
salary use EmpDb;
SELECT employee_id,first_name,last_name,salary,
CASE
WHEN salary >= 50000 THEN 'High Salary'
--problem statement --- Construct a Query to display the Publication, Language and count of
books(CountofBooks).... use DB_Publishers;
SELECT Publication,Language,COUNT(*) AS CountofBooks FROM TBL_Books
__________________________________________________________________________________
__ __________________
--problem tatement --- Find the total salary expenditure for the company(test case pass)
select sum(salary) as TotalExpenditure from EMP;
--problem statement --- write a sql query to display the top 10 genre names as "Name"
GROUP BY g.Name
SELECT
e1.EmployeeID,
e1.Year AS Year,
e1.CurrentSalary,
CASE
END AS PercentageIncrement
FROM
EmployeeSalaries e1
LEFT JOIN
EmployeeSalaries e2 ON
e1.EmployeeID = e2.EmployeeID
BY
e1.EmployeeID,
e1.Year;
--problem statement---find the total number of employees living in each particular address...
(EMPLOYEEDATA table name)
SELECT EMP_ADD, COUNT(*) AS NO_OF_EMPLOYEES FROM EMP
GROUP BY EMP_ADD
--problem statement--- A retail store wants to track its daily revenue and compare it with the
previous day's revenue.
SELECT RecordDate,
Revenue AS CurrentDayRevenue,
FROM DailyRevenue
END as grade_category
FROM STUDENTS order by
STUDENT_ID asc;
--problem statement--- write an Sql query to find the highest salary of each department
SELECT Dept_Name, MAX(Salary) AS Max_Sal
FROM Department
GROUP BY Dept_Name
__________________________________________________________________________________
__ _______________
--You have been given an Employees table that contains info about employees working in a
company. select distinct department_id from employees where salary < 50000 union
select distinct department_id from employees where salary >= 70000 order by
department_id asc;
select distinct department_id from employees where salary < 50000 or salary >= 70000 order by
department_id asc;
--Write a SQL query to select the required data from the employees table and include in a
column called generated_email.
select employee_id, first_name, last_name, email,
concat(left(first_name,3),left(last_name,4),'@example.com') as generated_email from
employees order by employee_id asc;
--Write a SQL query to display the names of employee concatinated with their job ids separated
by comma and space....
--Retrive the emp_name and salary of employees who have a salary greater than 30000 or whose
employee name starts with 'S'
select Emp_name, salary from EMP where salary > 30000 or Emp_name LIKE 'S%';
Q1
use DoSelect;
FROM Employee
OR
USE DoSelect;
FROM Employee
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q2
-> database name : EMPLOYEEDATA
problem statement : find the total salary expenditure for the company
use EMPLOYEEDATA;
SELECT
SUM(salary) AS TotalExpenditure
FROM
EMP;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q3
write a sql query to categorize employee into three groups based on their salary , high
salary, medium salary,low salary
use EmpDb;
SELECT
CASE
END AS salary_category
FROM
employees
ORDER BY
employee_id;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q4
-> database name: SocialDataUU
use SocialData;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q5
Problem statement : Retrive emp_name and salary of employees who have a salary greater than
$30000 or whose employee name starts with 'S'
* use EMPLOYEEDATA;
FROM EMP
OR
select Emp_name , Salary from EMP where Salary > 30000 or Emp_name like "S%" order by
Salary;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q6
use DB_Institute;
(SELECT E.STUDENTID
TBL_STUDENTS_FEES F ON
E.STUDENTID = F.STUDENTID
LEFT JOIN
TBL_STUDENTS_FEES F
ON E.STUDENTID = F.STUDENTID
ORDER BY E.STUDENTNM
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q7
problem statement : write a sql query to retrive a list of all customers and the total number
orders they have placed
use CustomerDb;
from customers as c
on c.customer_id = o.customer_id
group by c.customer_id
order by c.customer_id;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q8
use EmployeeDB;
PercentageIncrement
from EmployeeSalaries as e1
OR
e1.Salary AS CurrentSalary,
e2.Salary AS PreviousYearSalary,
FROM Employeesalaries as e1
OR
ORDER BY d.department_id;
OR
SELECT e1.EmployeeID, e1.Year,
e1.Salary AS CurrentSalary,
e2.Salary AS PreviousYearSalary,
FROM Employeesalaries e1
JOIN EmployeeSalaries e2
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q9
problem statement : You work for an ecommerce company that sells products in multiple
categorize the company has two tables categorize and products
use EmployeeDB;
category_name ASC;
OR
SELECT
Categories.category_name AS category_name,
Products.product_name AS product_name
FROM
Products
INNER JOIN
Categories
ON
Products.category_id = Categories.category_id
ORDER BY
Categories.category_name ASC;
OR
FROM Products as p
ON p.category_id=c.category_id
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q10
use EMPLOYEEDATA;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q11
problem statement :
use GradeDb;
SELECT student_id,first_name,last_name,grade,
END AS grade_category
from students
order by student_id;
OR
case
end as grade_category
from students
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
Q12. To display the top 10 genre names with the highest average unit price from the Track table
from Genre as g
join Track as t
on g.genreid = t.genreid
group by g.name
limit 10;
OR
FROM Track t
GROUP BY g.genre_name
LIMIT 10;
--------------------------------------------------------------------
SELECT
FROM Email_Data
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
FROM Locations L
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
15. EmployeeData Job id
FROM EmployeeData
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
FROM Emp;
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
FROM USERS U
LIMIT 4;
OR
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
18. Company Db =to retrieve a list of department_id, dept._name, and the first_name
USE CompanyDb;
FROM departments AS d
--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------
19. TBL_employee
USE DB_Company;
FROM TBL_Employee
LIMIT 1 OFFSET 2;