Running a simple “SELECT * ” in SQL can get you mounds and mounds of data from your database. Luckily, cleaning up the data output is as simple as adding a few parameters, such as a WHERE clause. You can even have your output provide a total count of specific data in your query. Just use the COUNT function as part of your SELECT statement. Let me show you how with a typical day-to-day data request.
Say your marketing department is looking to target previous customers for repeat sales. Its idea is to hit the city or state that has purchased the most widgets within the the last month–in this case, March of 2017. Table A shows the sales table in our database. Let’s use it to find out what state had the most sales logged in March 2017.
Table A: sales
We’ll use the sales table in our example query.
This table gives us ample data to work with. We just need to tailor our SQL syntax to meet our needs. Let’s build our query. To use the COUNT function, you include it in your SELECT statement with the field you’re trying to count in parentheses: COUNT(field name). You’ll also have to use the GROUP BY clause near the end of your query to organize the final output.
In our case, we’ll count the name field in the sales table to find out how many customers were in the various states that had sales. Here’s the syntax to find the count by shipping state in March 2017:
SELECT COUNT(name), state_code
WHERE date_of_purchase BETWEEN ’03/01/2017′ AND ’03/31/2017′
GROUP BY state_code;
The output should return the following data for your report:
If you look at the table and compare it to the results of the query, you’ll notice that the state of Texas only has a count of 1 even though the table has two records for that state. The variance is the date of the sale. Only one sale met the parameter of BETWEEN ’03/01/2017′ AND ’03/31/2017′ .
You can use the same strategy to find out the total number of widgets sold per state. Here’s your syntax:
SELECT COUNT (name), product, state_code
GROUP BY product, state_code;
This should return the following for your report:
A good place to start
Getting the count of a particular data set may be useful for forecasting or marketing in your enterprise. Understanding historical sales just may be fruitful for future sales. Counting and grouping your data is an easy way to begin your analysis. It’s true in business today that data is king. Be sure you’re able to poll your data with ease for optimal analysis using SQL.
- How to avoid big data project failures: Your 5-step guide
- SQL refresher: How to gather data from multiple tables
- SQL refresher: How to use the CASE function in a select statement to make your data clear
- Google adds new data analytics and database offerings
- Why this new school database might be your next data warehouse
Have you run into any obstacles when building your SQL queries? Share your questions and concerns with fellow TechRepublic members.