Book Image

Learn SQL Database Programming

By : Josephine Bush
5 (1)
Book Image

Learn SQL Database Programming

5 (1)
By: Josephine Bush

Overview of this book

SQL is a powerful querying language that's used to store, manipulate, and retrieve data, and it is one of the most popular languages used by developers to query and analyze data efficiently. If you're looking for a comprehensive introduction to SQL, Learn SQL Database Programming will help you to get up to speed with using SQL to streamline your work in no time. Starting with an overview of relational database management systems, this book will show you how to set up and use MySQL Workbench and design a database using practical examples. You'll also discover how to query and manipulate data with SQL programming using MySQL Workbench. As you advance, you’ll create a database, query single and multiple tables, and modify data using SQL querying. This SQL book covers advanced SQL techniques, including aggregate functions, flow control statements, error handling, and subqueries, and helps you process your data to present your findings. Finally, you’ll implement best practices for writing SQL and designing indexes and tables. By the end of this SQL programming book, you’ll have gained the confidence to use SQL queries to retrieve and manipulate data.
Table of Contents (24 chapters)
1
Section 1: Database Fundamentals
7
Section 2: Basic SQL Querying
11
Section 3: Advanced SQL Querying
16
Section 4: Presenting Your Findings
19
Section 5: SQL Best Practices

Using the HAVING clause

The HAVING clause is used to filter the GROUP BY results. The following code shows the HAVING syntax, where the WHERE and ORDER BY clauses are optional:

SELECT column(s) 
FROM table
WHERE condition(s)
GROUP BY columns(s)
HAVING condition(s)
ORDER BY column(s);

Learning how to use the HAVING clause to limit query results

Let's say you needed to see the results of running the GROUP BY clause in the previous section, but where the sum of at-bats is greater than 100. This is when you would use a HAVING clause, as shown in the following query:

USE lahmansbaseballdb; 
SELECT playerid, teamid, sum(AB) AS sum_at_bats
FROM batting
GROUP BY playerid, teamid
HAVING sum_at_bats > 100;

The following screenshot...