Upload
others
View
23
Download
0
Embed Size (px)
Citation preview
SQL Queries
Student ID First_Name Last_Name Expense Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 IPE234 Jessica Marlin 30000 1 feb 2018 CSE345 John Harli 21000 1 Jan 2018 IPE456 Peter Sharon 25000 1 feb 2018 EEE567 Anni Lee 27000 1 feb 2018 ETE
• Get all student details from student table.SELECT *FROM Student• Get First_Name, Last_Name from student table.SELECT First_Name, Last_nameFROM Student
Student
SQL Queries
Student ID First_Name Last_Name Expense Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 IPE234 Jessica Marlin 30000 1 feb 2018 CSE345 John Harli 21000 1 Jan 2018 IPE456 Peter Sharon 25000 1 feb 2018 EEE567 Anni Lee 27000 1 feb 2018 ETE
• Get First_Name from student table in UppercaseSELECT UPPER(First_Name)FROM Student• Get all student details from student table order by first name
ascending.SELECT *FROM Student ORDER BY First_Name ASC
Student
SQL Queries
Student ID First_Name Last_Name Expense Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 IPE234 Jessica Marlin 30000 1 feb 2018 CSE345 John Harli 21000 1 Jan 2018 IPE456 Peter Sharon 25000 1 feb 2018 EEE567 Anni Lee 27000 1 feb 2018 ETE
• Get student details, whose First_name is “John”SELECT *FROM StudentWHERE First_Name =‘John’• Get all student details from student table whose expense is less
than 25000SELECT *FROM StudentWHERE Expense < 25000
Student
SQL Queries
Student ID First_Name Last_Name Expense Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 IPE234 Jessica Marlin 30000 1 feb 2018 CSE345 John Harli 21000 1 Jan 2018 IPE456 Peter Sharon 25000 1 feb 2018 EEE567 Anni Lee 27000 1 feb 2018 ETE
• Get all student details from student table whose expense is in between 23000 and 30000
SELECT *FROM StudentWHERE Expense BETWEEN 23000 AND 30000• Get student details whose First_Name starts with ‘J’SELECT *FROM StudentWHERE First_Name LIKE ‘J%’
Student
SQL Queries
Employee ID
First_Name Last_Name Salary Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 SCM234 Jessica Marlin 30000 1 feb 2018 QC345 John Harli 21000 1 Jan 2018 SCM456 Peter Sharon 25000 1 feb 2018 Marketing567 Anni Lee 27000 1 feb 2018 R&D
• Increase Salary of all employees by 7% in the tableUPDATE EmployeeSET Salary + (Salary*7.0/100.0)
• Find number of employees working in the department ’SCM’SELECT Count (*)FROM EmployeeWHERE Department = ‘SCM’
Employee
SQL Queries
Employee ID
First_Name Last_Name Salary Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 SCM234 Jessica Marlin 30000 1 feb 2018 QC345 John Harli 21000 1 Jan 2018 SCM456 Peter Sharon 25000 1 feb 2018 Marketing567 Anni Lee 27000 1 feb 2018 R&D
• Print details of employees whose First_Name ends with R and contains 5 alphabets.
SELECT *FROM EmployeeWHERE First_Name LIKE ‘----R’
Employee
SQL Queries
Employee ID
First_Name Last_Name Salary Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 SCM234 Jessica Marlin 30000 1 feb 2018 QC345 John Harli 21000 1 Jan 2018 SCM456 Peter Sharon 25000 1 feb 2018 Marketing567 Anni Lee 27000 1 feb 2018 R&D
• Write the query to find out the highest Salary from the table.SELECT MAX (Salary)FROM Employee
• Write the query to find out the second highest Salary from the table.SELECT MAX (Salary)FROM EmployeeWHERE Salary < (SELECT MAX (Salary) FROM Employee)
Employee
SQL Queries
Employee ID
First_Name Last_Name Salary Joining_date Departmenrt
123 David Miller 23000 1 Jan 2018 SCM234 Jessica Marlin 30000 1 feb 2018 QC345 John Harli 21000 1 Jan 2018 SCM456 Peter Sharon 25000 1 feb 2018 Marketing567 Anni Lee 27000 1 feb 2018 R&D
Alternative ways to find the second highest Salary• LimitSELECT Salary FROM (SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT 2)AS Employee ORDER BY Salary LIMIT 1• TOPSELECT TOP 1 SalaryFROM (SELECT TOP 2 Salary FROM Employee ORDER BY Salary DESC)AS Employee ORDER BY Salary ASC
Employee
Practice ! Practice ! & Practice!
The End