Basic 'Select' Exercises: Mysql Task 3
Basic 'Select' Exercises: Mysql Task 3
--2. Select firstname, age and salary for everyone in your employee table.
SELECT FirstName,age,Salary from employee
--3. Selct firstname and display as 'Name' for everyone in your employee table
SELECT FirstName AS Name from employee
--4. Select firstname and lastname as 'Name' for everyone. Use " " (space) to separate
firstname and last.
SELECT CONCAT(FirstName,' ',LastName) AS Name from employee
--6. Select first and last names for everyone that's under 24 years old.
SELECT FirstName,LastName from employee WHERE Age<24
--7. Select first name, last name, and salary for anyone with "Programmer" in their
title.
SELECT FirstName,LastName,Salary from employee WHERE Title LIKE '%Programmer%'
--8. Select all columns for everyone whose last name contains "O".
SELECT * from employee WHERE LastName LIKE '%O%'
--9. Select the lastname for everyone whose first name equals "Kelly".
SELECT LastName from employee WHERE FirstName LIKE 'KELLY'
--10. Select all columns for everyone whose last name ends in "Moore".
SELECT * from employee WHERE LastName LIKE '%Moore'
--11. Select all columns for everyone who are 35 and above.
SELECT * from employee WHERE Age>=35
--13. Select firstname, title and lastname whose age is in the range 28 and 62 and
salary greater than 31250
SELECT FirstName,Title,LastName from employee WHERE Age BETWEEN 28 AND 62 AND Salary
>31250
--14. Select all columns for everyone whose age is not more than 48 and salary not less
than 21520
SELECT * from employee WHERE Age <48 AND Salary>=21520
--15. Select firstname and age of everyone whose firstname starts with "John" and
salary in the range 25000 and 35000
SELECT FirstName,Age from employee WHERE FirstName LIKE 'John%' AND Salary BETWEEN 25000
AND 35000
--17. Select all columns for everyone by their ages in ascending order.
SELECT * from employee ORDER BY Age ASC
--18. Select all columns for everyone by their salaries in descending order.
SELECT * from employee ORDER BY Salary DESC
--19. Select all columns for everyone by their salaries in ascending order.
SELECT * from employee ORDER BY Salary ASC
--20. Select all columns for everyone by their salaries in ascending order whose age not
less than 17.
SELECT * from employee WHERE Age>=17 ORDER BY Salary ASC
--21. Select all columns for everyone by their salaries in descending order whose age
not more than 34.
SELECT * from employee WHERE Age<=34 ORDER BY Salary DESC
--24. Show the results by adding 5 to ages and removing 250 from salaries of all
employees
SELECT FirstName,LastName,Title,(Age+5) as Age,(Salary-250) as Salary from employee
--25. Select the number of employees whose lastname ends with "re" or "ri" or "ks"
SELECT Count(*) as 'Number Of Employees' from employee WHERE LastName like '%re' OR
LastName LIKE '%ri' or LastName like '%ks'
--29. Select the average salary of employees whose age is not less than 35 and not more
than 50
SELECT AVG(Salary) 'Average of Salary' from employee where AGE BETWEEN 35 AND 50
--32. What is the combined salary that you need to pay to the employees whose age is not
less than 40
SELECT SUM(Salary) as Salary from employee where age >=40
--33. What is the combined salary that you need to pay to all the Freshers and
Programmers for 1 month
SELECT SUM(Salary) as 'Salary' from employee where title like 'Fresher' or Title like
'Programmer'
--34. What is the combined salary that you need to pay to all the Freshers whose age is
greater than 27 for 3years
SELECT (SUM(Salary)*12*3) from employee where title like 'Fresher' and age>27
SELECT FirstName,LastName,Age from employee where AGE =(SELECT MAX(AGE) from employee)
AND Salary<35000
--37. Select the eldest fresher whose salary is less than 35000
SELECT TOP 1 * from employee where title LIKE 'Fresher' and Salary<35000 ORDER BY Age
DESC
--38. Select firstname and age of everyone whose firstname starts with "John" or
"Michael" and salary in the range 17000 and 26000
SELECT FirstName, Age from employee where FirstName LIKE 'John' or FirstName LIKE
'Michael' and Salary BETWEEN 17000 AND 26000
------------------Using 'Group By' and 'Having' clause----------------
--39. How many employees are having each unique title. Select the title and display the
number of employees present in ascending order
SELECT Title, COUNT(*) AS 'Number of Employees' from employee GROUP BY Title ORDER BY
'Number of Employees' ASC
--40. What is the average salary of each unique title of the employees. Select the title
and display the average salary of employees in each
SELECT Title, AVG(Salary) as 'Average Salary' from employee GROUP BY Title
--43. In the age range of 25 to 40 get the number of employees under each unique title.
SELECT Title,COUNT(*) as 'Number of Employees'from employee where Age BETWEEN 25 and 40
GROUP BY Title
--44. Show the average salary of each unique title of employees only if the average
salary is not less than 25000
SELECT Title,AVG(Salary) AS Average from employee GROUP BY Title HAVING
AVG(Salary)>=25000
--45. Show the sum of ages of each unique title of employee only if the sum of age is
greater than 30
SELECT Title,SUM(Age) as 'Sum of Age' from employee GROUP BY Title HAVING SUM(Age)>30
-----------------------------Using 'Update'---------------------------
--• Lisa Ray just got married to Michael Moore. She has requested that her last name
be updated to Moore.
UPDATE employee Set LastName = 'Moore' where FirstName like 'Lisa' and LastName like
'Ray'
--• Ginger Finger's birthday is today, add 1 to his age and a bonus of 5000
UPDATE employee Set Age = Age + 1, Salary = Salary + 5000 where FirstName like 'Ginger'
and LastName like 'Finger'
--• All 'Programmer's are now called "Engineer"s. Update all titles accordingly.
Update employee Set Title = 'Engineer' where Title Like 'Programmer'
--• Everyone whose making under 30000 are to receive a 3500 bonus.
UPDATE employee Set Salary = Salary + 3500 where Salary<30000
--• Everyone whose making over 35500 are to be deducted 15% of their salaries
UPDATE employee Set Salary = Salary - 0.15*Salary WHERE Salary>35500