This way one can check if an ip is in the range of a given network. Pretty simple, yeah! But I had the need to query ip address that are not in a specific network range . Well there is no opposite operator – at least I couldn’t find one. So what I did is is the following:

SELECT COUNT(*) FROM ips WHERE CASE WHEN ip <<= inet '192.168.1/24' THEN false ELSE true END

Today I accidentally got to a piece of code I have written long ago and really got surprised from me. I have created a not conventional solution to a interesting problem. Imagine you have to add a bunch of rows into a db table, for example promo codes, that has column holding randomly generated strings. The strings should be unique of course, so we have unique key in the db! So instead of generating the string, checking if it is already in the table and if not -> adding it to the table, I have just written this: