10
SQL Queries Student ID First_Name Last_Name Expense Joining_date Departmenrt 123 David Miller 23000 1 Jan 2018 IPE 234 Jessica Marlin 30000 1 feb 2018 CSE 345 John Harli 21000 1 Jan 2018 IPE 456 Peter Sharon 25000 1 feb 2018 EEE 567 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_name FROM Student Student

SQL Queries - ahmedtoukir.com

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SQL Queries - ahmedtoukir.com

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

Page 2: SQL Queries - ahmedtoukir.com

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

Page 3: SQL Queries - ahmedtoukir.com

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

Page 4: SQL Queries - ahmedtoukir.com

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

Page 5: SQL Queries - ahmedtoukir.com

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

Page 6: SQL Queries - ahmedtoukir.com

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

Page 7: SQL Queries - ahmedtoukir.com

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

Page 8: SQL Queries - ahmedtoukir.com

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

Page 9: SQL Queries - ahmedtoukir.com

Practice ! Practice ! & Practice!

Page 10: SQL Queries - ahmedtoukir.com

The End