Download presentation
Presentation is loading. Please wait.
Published byAlison Powell Modified over 9 years ago
1
CS 111 – Nov. 10 Structured Query Language (SQL) –We’ve already seen simple select statements, with optional “where” clause and aggregate functions. –More example commands today Relational database Commitment –Review for test after lab
2
Log in As you will see in lab, you first need to “log in” to the database management system in order to see your database. From a Web browser, go to cs.furman.edu/phpMyAdmin/ Enter your database username & password (Demo) If your SQL commands are incorrect, you’ll get error message.
3
Example FirstLastLocationTitleSalary PeterJacobsBrusselsBroker55000 DeniseLambertBrusselsAccountant42500 RobertNijsBrusselsBroker66700 RuthMolloyChicagoManager68650 DeclanMurphyChicagoAccountant84125 SusanPattersonChicagoEconomist51000 RachelBradyCincinnatiBroker43300 DavidCunninghamCincinnatiAccountant48000 JohnWhelanCincinnatiBroker60500 YvonneButlerSan DiegoBroker48500 VeronicaKeatingSan DiegoBroker72000 MaryWalshDublinAccountant46850 An abridged Employee table:
4
Distinct Sometimes in SQL you have a lot of repeated data. And you only want the values themselves, not the repetitions. –Ex. 70, 70, 70, 80, 80, 80, 80, 80, 100 70, 80, 100 Examples select Test2 from Student; gives all test grades select distinct Test2 from Student; only shows each value once select distinct Test1, Test2 from Student; finds distinct pairs. 70/60, 70/80, and 90/80 are considered distinct.
5
Group by The “group by” clause is good at finding subtotals. Example: how many employees by job title: –Select count(first) from Employee group by Title; –Actually, doesn’t matter which field we count. To make output easier to understand, we should also print out the job titles: –Select Title, count(Salary), avg(Salary) from Employee group by title; We can even subtotal by 2 fields: What would this command mean? –Select Location, Title, count(Salary) from Employee group by Location, Title;
6
Order by This clause is used for sorting. Default order is ascending. select * from Employee order by Last; select * from Employee order by Location, Last; select * from Employee where Salary > 50000 order by Location, Title;
7
More on “where” Boolean conditions: when you use the “where” clause can include the word “and” or “or” to make complex conditions. –Ex. What if we wanted salaries of employees with names starting with M or P. Use “in” when you want to select among several possible values. Has the same effect as “or” –Select * from Employee where Location in (“Dublin”, “Chicago”); Use “between” for an (inclusive) range of values to check –Select * from Employee where Salary between 60000 and 70000;
8
Relational Database Databases with just 1 table are not very powerful. More interesting if 2 tables are related. –Books and publishers –Customers and orders –Pets and owners Typically we have a “one-to-many” relationship The two tables need to have a field in common. –You can see this if you try to list the necessary fields in the above examples. In SQL, to refer to a field within some table, we use the dot notation: Customer.First, Pet.Name, Order.ID
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.