Subscribe to DSC Newsletter

3 Essential SQL Queries in 1.5 Minutes

How often are you stuck waiting for someone else to pull data for you? There's nothing worse than missing a deadline because someone else didn't do a 30 second query. Never again - here are the basic queries that will get you numbers instantly for pivoting, graphing, and other applications of your analysis skills, as applied to an imaginary table of sales data:
 
Get It All

  select * from sales
 
In SQL, the simplest queries are often the most powerful. This grabs every row and every column. If the table is under 50,000 rows, you should have no problem opening it in Excel. If it's bigger, the program may slow down, depending on your RAM and what other applications are running.
 
Get Columns and Rows of Interest

  select date, revenue, salesperson from sales where region = 'South'
 
If your organization has too much data to pull down all at once, it's easy enough to work around. Often, you'll be looking for specific slices of the data. It may be by date, salesperson, or even a combination of the two. Again, this simple query returns *almost* all the data, giving you freedom to explore in Excel.
 
Instant Pivot Table

  select salesperson,sum(revenue) total_revenue from sales group by salesperson
 
This query is a little more complex, but may instantly provide one of your first insights. It's doing exactly the same thing as a pivot table in Excel, summing the revenue for each salesperson. Once you become comfortable with this query, you're well on your way to more advanced functionalities within SQL!
 
That's it! I hope that reading this quick introduction pays off in spades.

Matthew Ritter writes practical, action-oriented data advice at Preinvented Wheel

Final tip: This reference will only be valuable if you keep it. Bookmark it, print it, email it to yourself, tweet it and favorite it, or whatever else the cool kids are doing these days.

Views: 1526

Comment

You need to be a member of Data Science Central to add comments!

Join Data Science Central

Videos

  • Add Videos
  • View All

© 2019   Data Science Central ®   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service