BIS05 Introduction to SQL

3,040
-1

Published on

Course Material for MBA Course on Business Information Systems

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,040
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
289
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

BIS05 Introduction to SQL

  1. 1. Business Information Systems Introduction to SQL Prithwis Mukerjee
  2. 2. Categories of SQL <ul><li>Data Retrieval </li></ul><ul><ul><li>Select </li></ul></ul><ul><li>Data Manipulation Language (DML) </li></ul><ul><ul><li>Insert </li></ul></ul><ul><ul><li>Delete </li></ul></ul><ul><ul><li>Update </li></ul></ul><ul><li>Transaction Control </li></ul><ul><ul><li>Commit </li></ul></ul><ul><ul><li>Rollback </li></ul></ul><ul><li>Data Definition Language (DDL) </li></ul><ul><ul><li>Create </li></ul></ul><ul><ul><li>Alter </li></ul></ul><ul><ul><li>Drop </li></ul></ul><ul><li>Data Control </li></ul><ul><ul><li>Grant </li></ul></ul><ul><ul><li>Revoke </li></ul></ul>
  3. 3. Basics of Retrieval - 1 : Projection & Selection <ul><li>Projection …. [ a subset of COLUMNS ] </li></ul><ul><ul><li>Select Emp_ID, LastName, Salary from Employee </li></ul></ul><ul><ul><li>Select * from Employee </li></ul></ul><ul><li>Selection … [ a subset of ROWS ] </li></ul><ul><ul><li>Select * from Employee where Dept = ’40’ </li></ul></ul>
  4. 4. Basics of Retrieval – 2 : JOIN <ul><li>Select EmpID, LastName, FirstName, Location from Employee, Department where Employee.Dept = Department.Dept_ID </li></ul>JOIN
  5. 5. The Retrieval Process Select EmpID, LastName, FirstName, Location from Employee, Department where Employee.Dept = Department.Dept_ID SQL statement entered through a program Output returned to another program OR screen Actual Data stored in the RDBMS ‘engine’
  6. 6. Some ‘Select’ statements <ul><li>Select * from Employees; </li></ul><ul><li>Select LastName, JobDesc from Employees ; </li></ul><ul><li>Select distinct JobDesc from Employees; </li></ul>
  7. 7. ‘Where’ : to eliminate some rows <ul><li>Select LastName, FirstName, JobDesc where JobDesc = ‘Clerk’; </li></ul><ul><li>Select LastName, FirstName, Salary where Salary > 35000; </li></ul><ul><li>Select LastName, Firstname where JobDesc <> ‘Clerk’; </li></ul>
  8. 8. Do you LIKE wildcards ? <ul><li>Select LastName, FirstName, JobDesc from Employees where LastName like ‘D%'; </li></ul><ul><li>Select LastName, FirstName, JobDesc from Employees where LastName like ‘_ _ _ _ i _’; </li></ul><ul><li>Select LastName, FirstName, JobDesc from Employees where LastName like ‘ _ _ _ _ i%’; </li></ul>
  9. 9. IN - BETWEEN <ul><li>Select LastName, FirstName, JobDesc, Salary from Employees where Salary is between 10000 and 20000; </li></ul><ul><li>Select LastName, FirstName JobDesc, Dept from Employees where Dept in (’40’, ’20’, ’30’); </li></ul>
  10. 10. Order ! Order !! <ul><li>Select LastName, FirstName, JobDesc, JoinDate from Employees ORDER BY JoinDate; </li></ul><ul><li>Select LastName, FirstName, JobDesc, Dept from Employees where Dept in (’20’,’30’,’40’) order by Dept; </li></ul>
  11. 11. More Restrictions : AND <ul><li>Select LastName, FirstName, JobDesc, Salary, Dept from Employees where Dept = ’20’; </li></ul><ul><li>Select LastName, FirstName, JobDesc, Salary, Dept from Employees where Dept = ’20’ and Salary > 10000; </li></ul><ul><li>Select LastName, FirstName, JobDesc, Salary, Dept from Employees where Dept = ’20’ and Salary > 10000 and JobDesc = ‘Clerk’; </li></ul><ul><li>No rows returned ! </li></ul>
  12. 12. Less Restrictions : OR <ul><li>Select LastName, FirstName, JobDesc, JoinDate, Salary from Employees where JobDesc like ‘%Manager’ OR Salary >= 25000; </li></ul><ul><li>Select LastName, FirstName, JobDesc, JoinDate, Salary from Employees where JobDesc like ‘%Manager’ OR Salary >= 25000 OR FirstName = ‘Madhuri’ </li></ul>
  13. 13. Matters of Precedence <ul><li>Select LastName, FirstName, JobDesc, Salary where JobDesc = ‘Cus_Rep’ OR JobDesc = ‘Clerk’ AND Salary < 16000; </li></ul><ul><li>Select LastName, FirstName, JobDesc, Salary where (JobDesc = ‘Cus_Rep’ OR JobDesc = ‘Clerk’) and Salary < 16000; </li></ul>
  14. 14. Functions … <ul><li>Two kinds of functions …. </li></ul><ul><ul><li>Single row functions </li></ul></ul><ul><ul><ul><li>Manipulate data items from one row </li></ul></ul></ul><ul><ul><ul><li>Accepts multiple arguments and return one value </li></ul></ul></ul><ul><ul><ul><ul><li>Arguments can be column value or expression </li></ul></ul></ul></ul><ul><ul><ul><li>Acts on one row </li></ul></ul></ul><ul><ul><ul><li>Returns one result per row </li></ul></ul></ul><ul><ul><ul><li>Can be nested </li></ul></ul></ul><ul><ul><li>Multiple row functions </li></ul></ul><ul><ul><ul><li>Works with data items from multiple rows </li></ul></ul></ul><ul><ul><ul><ul><li>Arguments can be column value or expressions </li></ul></ul></ul></ul><ul><ul><ul><li>Returns ONE result for a set of rows </li></ul></ul></ul>
  15. 15. Single Row Functions <ul><li>Select EmpID, Salary, Comm, Salary+Comm*Salary from employee where DeptID = ’30’; </li></ul><ul><li>Select concat (‘Mr.’, Trunc(FirstName,1,1),’.’,LastName) from Employee where EmpID = ‘742866’; </li></ul><ul><li>Mr. A.Bacchan </li></ul>
  16. 16. Multiple Row Functions .. <ul><li>Select sum(salary) from Employee; </li></ul><ul><li>Select avg(salary) from Employee; </li></ul><ul><li>Select max(salary) from Employee; </li></ul><ul><li>Other functions </li></ul><ul><ul><li>Count </li></ul></ul><ul><ul><li>Min </li></ul></ul><ul><ul><li>And so on … </li></ul></ul>
  17. 17. MultiRow Functions : Group By & Having <ul><li>Select Dept, sum(salary) from Employee Group by Dept; </li></ul><ul><li>Select Dept, sum(salary) from Employee Group By Dept having Sum(salary) < 100000 </li></ul>
  18. 18. Variety of Single Row Functions <ul><li>Case manipulation </li></ul><ul><ul><li>Lower </li></ul></ul><ul><ul><li>Upper </li></ul></ul><ul><li>Character manipulation </li></ul><ul><ul><li>Concat </li></ul></ul><ul><ul><li>Substr </li></ul></ul><ul><ul><li>Length </li></ul></ul><ul><ul><li>Lpad / Rpad </li></ul></ul><ul><ul><li>Etc </li></ul></ul><ul><li>Number functions </li></ul><ul><ul><li>Round </li></ul></ul><ul><ul><li>Trunc </li></ul></ul><ul><ul><li>Mod </li></ul></ul><ul><li>Date Arithmetic </li></ul><ul><ul><li>Months_Between </li></ul></ul><ul><ul><li>Next_Day </li></ul></ul><ul><ul><li>Add_Months </li></ul></ul><ul><li>Data Type conversion </li></ul><ul><ul><li>To_number </li></ul></ul><ul><ul><li>To_date </li></ul></ul><ul><ul><li>To_char </li></ul></ul>This is only an illustrative subset. There are many more each with its own syntax ….
  19. 19. Join : accessing data from two tables <ul><li>Select EmpID, LastName, FirstName, Location from Employee, Department where Employee.Dept = Department.Dept_ID </li></ul><ul><li>What happens if we miss the JOIN condition ? </li></ul>
  20. 20. Cartesian Product <ul><li>Let us work with a slightly simpler table … </li></ul><ul><li>Select FirstName, LastName, DepartName from Employee, Department; </li></ul><ul><ul><li>We get the Cartesian Product </li></ul></ul><ul><ul><li>This can lead to an enormously large number of rows </li></ul></ul><ul><li>The JOIN condition is very important </li></ul>
  21. 21. The Outer Join <ul><li>The common join is the Inner join </li></ul><ul><ul><li>Where we pull out rows from both tables that have something ( the join key value ) in common </li></ul></ul><ul><li>But what if there are rows in one table that do not have the common value … we use Outer Join </li></ul><ul><li>The syntax for performing an outer join in SQL is database-dependent. For example, in Oracle, we will place an &quot;(+)&quot; in the WHERE clause on the other side of the table for which we want to include all the rows. </li></ul>
  22. 22. Outer Join <ul><li>Select FirstName, JobDesc, Dept_Name from Employee, Department where Employee.Dept (+) = Department.Dept_Id; </li></ul><ul><li>Select FirstName, JobDesc, Dept_Name from Employee, Department where Employee.Dept = Department.Dept_Id (+) ; </li></ul>
  23. 23. Full Outer JOIN <ul><li>Select FirstName, JobDesc, Dept_Name from Employee, Full Outer Join Department where Employee.Dept = Department.Dept_Id; </li></ul>
  24. 24. SubQuery : An Alternate to JOIN <ul><li>Select FirstName, JobDesc from Employee where Dept IN (Select Dept_ID from Department); </li></ul><ul><li>Select FirstName, JobDesc from Employee, Department where Employee.Dept = Department.Dept_ID </li></ul>
  25. 25. SELF JOIN : Joining a Table to itself <ul><li>Suppose we have this table where we have an additional column called Manager that stores Manager ID </li></ul><ul><ul><li>There is one person who does not report to anyone else. </li></ul></ul><ul><ul><li>Everyone else reports to department manager. </li></ul></ul><ul><ul><li>Department managers report to Executive </li></ul></ul><ul><li>We want to know who report to whom </li></ul>
  26. 26. Self join <ul><li>Select w.FirstName, w.JobDesc, w.Dept , ’reports to’, m.FirstName,m.JobDesc,m.Dept from Employee w , Employee m where w.Manager = m.EmpID </li></ul>
  27. 27. Insert .. <ul><li>Insert into Department (’50’,’Research’,’123456’,’Pune’) </li></ul>
  28. 28. Update .. <ul><li>Update Department set Manager = ‘123456’ where Dept_ID = ’40’; </li></ul>
  29. 29. Delete .. <ul><li>Delete from Department where DeptID = ’40’; </li></ul>
  30. 30. Create View <ul><li>Create View myView as ( </li></ul><ul><ul><li>Select ..... </li></ul></ul><ul><ul><li>any SQL query </li></ul></ul><ul><ul><li>simple or complex </li></ul></ul><ul><ul><li>that we have </li></ul></ul><ul><ul><li>learnt about so far .. </li></ul></ul><ul><ul><li>) </li></ul></ul><ul><li>Select * from myView ; </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×