SQL #3: More Constraints

Last post I went over some constraints. Somebody corrected me that they were actually conditions or filters. Even after searching, they still seem like the same thing. Either way, they’re both boundaries I’m setting for the selector. I’m not yet gonna say they can be used interchangeably, but for now we’ll stick with constraints.

So back to my basketball team. Let’s say I want to see all my players whose field goal percentage starts with 8, so the 80 percenters.

SELECT player, fgp FROM bball
WHERE fgp LIKE "8%";

The percentage in this command is called a wildcard character. This tells the selector to include any sequence of characters that follow that 8. If I just put 8 by itself, it would only look for the number 8. The percentage will search for any field goal percentage starting with 8. In this context, that should be strictly 80 percenters, unless there’s a player with an 8% field goal rate, which if he’s still on the team, I’d assume he’s a damn good passer or defender. But you get the point.

One more thing with this. An underscore is also a wildcard character, but it’s only used to specify the following characters after your beginning one. So if I put 1 underscore there instead of the percentage, that solves my problem of leaving out the player with the 8%, and it will still look for 80 percenters. But, some player’s numbers might have a decimal, like 82.3. If I only put that 1 underscore, that leaves out players with the decimals, because it’s only looking for 1 character after that 8. ‘.3’ is 2 more characters. So for this context, since I’m looking for the 80 percenters overall, the percentage sign was more appropriate.

You see how anal this gets, but we have to consider these things when looking for data. Like I said, the bare minimum is my enemy with this field. I can’t rely on doing ‘just enough’.

Next, we got a simple one. I want to view all my shooting guards (SG):

SELECT player, position FROM bball
WHERE position = "SG";

The equal sign takes care of that.

Let’s flip it. All my players who are NOT shooting guards.

SELECT player, position FROM bball
WHERE position != "SG";

The exclamation point behind the equal sign flips it to not equal to

Leave a comment