Live data from Hacker News

PostgreSQL Exercises

pgexercises.com

21–30 of 47 posts

Re: PostgreSQL Exercises

#21

Zipcode is an integer? What about zips in New Jersey?

You can store a NJ zip in an integer; you just have to be careful when printing it. Annoying, but if space is scarce, maybe worth it. (Though if space is too scarce for zipcodes, good luck storing anything else more interesting.)

Re: PostgreSQL Exercises

#22

Earlier quoted context omitted.

As a general rule of thumb, if something isn't used for calculations, it probably shouldn't be a number.

That wouldn't generally be my approach. In my view if something is a number it should be typed as such. That said, I recognise that there are pros and cons to this approach similar to those in strongly vs weakly typed PLs. My personal preference is for a strong schema.

There is a hidden assumption in your phrase "if something is a number". You imply that a zipcode is "obviously" a number. But it's not obvious. A zipcode is obviously a sequence of digits, but not everything that we represent as a sequence of digits is meaningfully treated as a number.

Leading zeros are one sign that zip codes are just digit strings, not numbers. Another (silly) example: if you REALLY have a number, then it doesn't matter what base you write it in. The number four can be written as 4 in base ten, or as 100 in base two. If you write a zip code in anything other than base ten, it's not really a zip code anymore.

Likewise, you can't add zip codes, nor subtract them, nor even meaningfully say that 11229 is more than 11228. You might as well use the letters A through J instead of the digits zero through nine, and suffer no serious inconvenience.

And indeed, some countries use something like zip codes, except that they have letters and numbers, yet I would hesitate to say that Canadian zip codes are so fundamentally different from US zip codes that they deserve to be stored under a different data type.

In conclusion, even you prefer a strong schema, a case can be made that zip codes should not be integers. Of course, that leaves open the question of what they should be, since most SQL databases have no built-in data type for digit sequences or anything similar. I have no answer to this and would personally just use integers anyway.

Re: PostgreSQL Exercises

#23
post #19

This seems as good of place as any to ask novice-level questions about PostgreSQL. I teach students SQLite and one of the most massive pain points in transitioning to PgSQL is how the latter, in the `WHERE` clause, fails to recognize aliases in the `SELECT` clause, e.g. SELECT UPPER(name) AS bigname FROM people WHERE bigname = 'JOE'; https://stackoverflow.com/questions/38040631/postgresql-does... Apparently this is t…

It's because the WHERE clause is evaluated before the SELECT. The reason that SQLite allows you to do that is because SQLite is much, much simpler than PgSQL, MySQL and SQL Server (all of which don't allow you to do this).

http://tinman.cs.gsu.edu/~raj/sql/node22.html

Re: PostgreSQL Exercises

#24

Earlier quoted context omitted.

As a general rule of thumb, if something isn't used for calculations, it probably shouldn't be a number.

That wouldn't generally be my approach. In my view if something is a number it should be typed as such. That said, I recognise that there are pros and cons to this approach similar to those in strongly vs weakly typed PLs. My personal preference is for a strong schema.

So you'd create a different table for international addresses, or have 2 zip code fields or ...?

Re: PostgreSQL Exercises

#25
post #19

This seems as good of place as any to ask novice-level questions about PostgreSQL. I teach students SQLite and one of the most massive pain points in transitioning to PgSQL is how the latter, in the `WHERE` clause, fails to recognize aliases in the `SELECT` clause, e.g. SELECT UPPER(name) AS bigname FROM people WHERE bigname = 'JOE'; https://stackoverflow.com/questions/38040631/postgresql-does... Apparently this is t…

MSSQL has the same (very frustrating) limitation. The obvious and ugly solution would be to wrap in a subquery (bonus: still std sql compliant, and performs well):

    SELECT bigname
    FROM (
        SELECT UPPER(name) AS bigname
        FROM people
    ) x
    WHERE bigname = 'JOE'
Add columns as necessary, it should work fine.

But this method can make large queries with lots of aliasing very difficult to read and lead to deep nesting. There is an alternative in MSSQL and (via a quick search) pgsql as well to keep the crazy nesting to a minimum:

    -- MSSQL
    SELECT bigname
    FROM people
    CROSS APPLY (SELECT UPPER(name) as bigname) x
    WHERE bigname = 'JOE'

    -- PGSQL (not 100% sure of syntax)
    SELECT bigname
    FROM people
    LEFT JOIN LATERAL (SELECT UPPER(name) as bigname) x on true
    WHERE bigname = 'JOE'
The problem is this might have a negative impact on performance because it mucks with the query planner. I've seen everything from huge, complex, CROSS APPLYs have literally zero performance penalty to completely benign single-column renames tanking query performance 10x, so beware if you use this.

Re: PostgreSQL Exercises

#26
post #19

This seems as good of place as any to ask novice-level questions about PostgreSQL. I teach students SQLite and one of the most massive pain points in transitioning to PgSQL is how the latter, in the `WHERE` clause, fails to recognize aliases in the `SELECT` clause, e.g. SELECT UPPER(name) AS bigname FROM people WHERE bigname = 'JOE'; https://stackoverflow.com/questions/38040631/postgresql-does... Apparently this is t…

It's because the WHERE clause is evaluated before the SELECT. The reason that SQLite allows you to do that is because SQLite is much, much simpler than PgSQL, MySQL and SQL Server (all of which don't allow you to do this). http://tinman.cs.gsu.edu/~raj/sql/node22.html

That seems straightforward enough, don't know why I assumed SQLite's implementation of it was a feature as opposed to a consequence of its overall simplified model. I think I assumed/hoped that aliasing worked as a kind of function definition which WHERE could magically understand and evaluate.

Re: PostgreSQL Exercises

#27
post #22

Earlier quoted context omitted.

That wouldn't generally be my approach. In my view if something is a number it should be typed as such. That said, I recognise that there are pros and cons to this approach similar to those in strongly vs weakly typed PLs. My personal preference is for a strong schema.

There is a hidden assumption in your phrase "if something is a number". You imply that a zipcode is "obviously" a number. But it's not obvious. A zipcode is obviously a sequence of digits, but not everything that we represent as a sequence of digits is meaningfully treated as a number. Leading zeros are one sign that zip codes are just digit strings, not numbers. Another (silly) example: if you REALLY have a number,…

What's the issue with making them char(5) (or perhaps char(9) depending if you want to do zip+4)?

That's what I've always done..

Re: PostgreSQL Exercises

#28

Earlier quoted context omitted.

As a general rule of thumb, if something isn't used for calculations, it probably shouldn't be a number.

That wouldn't generally be my approach. In my view if something is a number it should be typed as such. That said, I recognise that there are pros and cons to this approach similar to those in strongly vs weakly typed PLs. My personal preference is for a strong schema.

Zip codes aren't numbers, they are numeric strings.

Re: PostgreSQL Exercises

#29
post #2

Great idea, does what it should and can be extended to cover a lot more. Keep up the great work. First time looking at it I thought I was on a official Ubuntu site, what a font and color can do to a branding is fascinating.

Author here: out of curiosity, what would be your priorities for future exercises? Right now my list looks roughly like: - DDL - Advanced datatypes (JSON, arrays, etc) - GIS This order is partly informed by the current support of the site: adding DDL exercises is a relatively minor bit of technical work, and then it's just writing. Everything else involves schema changes (or, possibly, a second database).

The order sounds reasonable - IMHO it correlates with the number of users who need that area of knowledge.

Re: PostgreSQL Exercises

#30
post #22

Earlier quoted context omitted.

That wouldn't generally be my approach. In my view if something is a number it should be typed as such. That said, I recognise that there are pros and cons to this approach similar to those in strongly vs weakly typed PLs. My personal preference is for a strong schema.

There is a hidden assumption in your phrase "if something is a number". You imply that a zipcode is "obviously" a number. But it's not obvious. A zipcode is obviously a sequence of digits, but not everything that we represent as a sequence of digits is meaningfully treated as a number. Leading zeros are one sign that zip codes are just digit strings, not numbers. Another (silly) example: if you REALLY have a number,…

it's not 'a case can be made' - it's strictly incorrect to store zip codes as integers
Post reply on HN