12 ms·
1. If you are a a programmer I think you should learn SQL. It will give you a different perspective on programming that I think is invaluable. (I.e. programming
by fifilura 3y ago
1. If you are a a programmer I think you should learn SQL. It will give you a different perspective on programming that I think is invaluable. (I.e. programming without for loops)
2. Combining and slicing data is a craft, and doing it subtly wrong in one step can lead to fatal errors in the outcome.
And most importantly, it can be very difficult to notice. Numbers don't smell.
That is why I would be very hesitant to give a slightly more than trivial task to an engine that fails 25% of the time.
But I guess that is the same as any other programming task. Just that other programming tasks require a lot of boilerplate where an AI can help. SQL is much more straight to it.
Maybe it could be useful to ask questions that are similar to writing testcases "how can I verify that my query is doing the right thing"?
- gavinray 3y agoWhat makes you think that SQL doesn't have "for loops"? Ever heard of LATERAL joins/CROSS APPLY? SELECT loop.value, x.squared FROM generate_series(1,5) AS loop(value) CROSS JOIN LATERAL (SELECT loop.value * loop.value AS squared) AS x;
- neuronexmachina 3y agoTIL
- gavinray 3y agoThey're incredibly useful -- any time that you want to both: - Reference data from the previous part of the query (the "left-hand side") - Return multiple columns The only way you can achieve it is with LATERAL/CROSS APPLY. Regular correlated subqueries can only return a single column, so something like this doesn't work: SELECT loop.val, (SELECT loop.val * loop.val, 'second column') AS squared FROM (SELECT loop.val FROM generate_series(1,5) AS loop(val)) as loop You'd get: error: subquery must return only one column
- ttfkam 3y agoI love LATERALs, but this still fits within set theory and a bulk application rather than an iterative for-loop. It may even be implemented as a for-loop within the engine, but SQL being declarative abstracts that away from the query interface. It's sets all the way down. A set of f(x) is still a set.
- gavinray 3y agoLets get even more cursed then: CREATE TEMP TABLE temp_results(value int, value_squared int); DO $$ DECLARE r int; BEGIN FOR r IN SELECT generate_series FROM generate_series(1,5) LOOP INSERT INTO temp_results VALUES (r, r * r); END LOOP; END$$; SELECT * FROM temp_results;
- fifilura 3y agoUw you win, I guess. Already started to think what I would write in that code review comment ;)
- koolba 3y agoAt the very least mention that re-running the SELECT in the same connection would include the prior results as well because they are preserved across commands within the same connection.
- ttfkam 3y agoHa! plpgsql's seemingly sole purpose is to inject imperative code into a set-based environment. Probably does it more smoothly than most pl languages, but that's at the cost of imperative clarity. But you're right. Postgres does allow for-loops like this. (They're also slower than the equivalent set-oriented approach.)
- samstave 3y agoIf anyone is interested, I had gpt explain this SQL to me - and it was really helpful, as I couldnt parse that with my level of SQL... https://chat.openai.com/share/931b1778-6393-4e86-94b4-b3b5a594917e https://chat.openai.com/share/931b1778-6393-4e86-94b4-b3b5a5...
- fifilura 3y agoThis is not what I call a for loop but more akin to a list comprehension. And this is the point I was trying to make. Instead of start with the "how", learn to do the "what".
- 3rd3 3y agoWhat is the point of the cross join? This would work as well: SELECT loop.value, loop.value * loop.value FROM generate_series(1,5) AS loop(value)
- ComputerGuru 3y agoFor this example, nothing. It would be useful where neither of the two SELECT queries is a subset/superset of the other. (Not saying you didn't know that.)
- 3rd3 3y agoCould you give an example?
- ako 3y agoThis will be useful if you have a table with some related history records, e.g., products with product price history, and you want to get the latest price. The lateral join would get all prices for the current product, sort them by date and then pick the top row.
- beefield 3y agoI don't think that the point was that there are no for loops in SQL. I think the point was that almost always using for loops is wrong and super inefficient and there is a much more efficient way to just use joins instead.
- fijiaarone 3y agoI wonder if anyone realizes that selects and joins in every database are implemented literally with for loops written in c. The reason people use a relational database is because it has loops that are faster, safer, and more efficient than anything you can write.
- fifilura 3y agoWhat does it matter? Yeah, at the very bottom of it all there will be a loop. I wouldn't attribute that to C but to the von Neumann architecture. The point is that by getting rid of loops you remove one way of telling the computer "How" to do it. Start here, do it in this order, combine the data this way. When "How" is a solved problem, it is a waste of your brain to think about that again. "What" is a better use of your brain cycles.
- ako 3y agoExactly, SQL is like a very specific natural language used to tell a database what data you need. The database contains AI (the query optimizer) that will use statistics to determine the fastest way to retrieve the data requested. Depending on your query and the data you have the fastest way to get your data will change. It’s hard to manually write this yourself.
- riku_iki 3y ago> joins in every database are implemented literally with for loops written in c. different kind of loops can be different, e.g. 2 nested loop with quadratic time: for i in t1: for j in t2: vs sort + merge join with n log n time.
- ComputerGuru 3y agoLATERAL first available in PostgreSQL 9.3 (2013), but still not available in SQLite.
- fifilura 3y agoSQLite has RECURSIVE, so you can generate a table with all numbers using something like: WITH RECURSIVE cnt(x) AS ( SELECT 1 UNION ALL SELECT x+1 FROM cnt LIMIT 5 ) SELECT x FROM cnt; And then do a regular CROSS JOIN on that table.
- ComputerGuru 3y agoBut generating a table with all numbers was just a throwaway example of the LATERAL JOIN syntax/use, and (hopefully) not what you'd actually use it for in the real world. It's not clear to me that (mathematically) a lateral join can be reduced to a recursive cte (and if the performance of a recursive cte would be acceptable for the cases where it does work as a substitute).
- rgbrgb 3y agoeveryone (not just programmers) should learn SQL, and I have many times! For me, programming languages are pretty different from bikes in that I have very little "muscle memory" for syntax when staring at a blank page, especially when the bikes I ride more often feel totally different. Having my intern instantly write an almost correct draft of a function/query helps a lot. > Maybe it could be useful to ask questions that are similar to writing testcases "how can I verify that my query is doing the right thing"? that seems like a good thread to pull on!
- calvinmorrison 3y agoHonestly I would rather write stuff in code thats inefficient than deal with one more stored procedure. Its like a black box
- fifilura 3y agoJust that deeply inside that forest of functions you just wrote is the inner join that the SQL query would do in a couple of lines of code embedded.
- calvinmorrison 3y agomore like, when it comes to complex data structures and logic, i will do that outside of sql. I'll do a join with sql no problem, by the time we're doing multiple inner joins I usually prefer to just do multiple sql queries. I don't care about performance that badly.
- fifilura 3y agoI usually only do one join at a time. But I separate them with CTEs ("WITH"). I can agree that many joins at once can make you grow grey hair.
- ako 3y agoThat’ll often not scale to millions of records. Letting the database optimizer find the optimal execution path instead of doing it procedurally elsewhere might result in “finishes in 5 minutes”, versus “doesn’t fit in a night”.
- threeseed 3y agoThis isn’t the 90s. Most hardware is way over-specced for the data sizes most people are dealing with. The number of use cases which are too heavy to finish in hours but small enough to fit in a single instance is pretty limited.
- 3y ago
- brightball 3y agoI’ve been working in this industry professionally for close to 25 years now. By far the most valuable and useful language I have learned is SQL.
- pavlov 3y agoAs a counterpoint, I’ve also worked in this industry for 25 years, and SQL has had no relevance to my career. I write it very occasionally, and if I didn’t, it wouldn’t make any difference. The C for loop on the other hand…
- fifilura 3y agoI am happy for you, you seem to have reached your local optimum!
- threeseed 3y agoI actually work in the Data/AI space and SQL has been of limited use to me. Whilst Snowflake is pretty popular the days of elaborately modelled EDWs are long gone. And so typically I find I am doing queries, transformations etc in some abstraction layer e.g. Spark on a data lake, ORM for web applications etc.
- tomnipotent 3y ago> the days of elaborately modelled EDWs are long gone They're more prevalent than ever in my experience. Consider the popularity of dbt.
- hobs 3y agodbt is not elaborately modeled at all, it eschews all the up front work of data modeling with ... well basically a lot of code and poorly maintained balls of mud. Don't forget the models other's create for you - often hilariously slow code to present a set of facets that often barely align with your business delivery needs; and don't forget to sync it even more slowly with Fivetran, the DE platform of the future!
- aussieguy1234 3y agoFor those people who would rather use an ORM, its worth mentioning that ORMs write very bad, un-performant SQL under the hood. They may save you a bit of time initially but when your company gets bigger, the ORMs will become a bottleneck.
- abhibeckert 3y ago"Bad" and "un-performant" are relative terms and as your company gets bigger, you're increasingly more and more likely to have colleagues who write even worse queries than an ORM would. For example I've encountered queries that are not only slow, but they generate several hundred megabytes of output all of which is sent to the user's web browser where JavaScript selects the relevant two kilobytes of data to show the user. The worst I've ever seen was a system where every single write to the database would be sent to every single web browser viewing certain webpages. 99.999999% of the writes were completely irrelevant and javascript in the browser would simply disregard them. The server load was immense... and eventually our Sysadmin brought it to someone's attention. Where we found out it was leaking sensitive data.
- aussieguy1234 3y agoI guess once you get to a certain point, you need to hire engineers that have strong SQL skills in order to scale.
- klibertp 3y ago> They may save you a bit of time initially but when your company gets bigger, the ORMs will become a bottleneck. At which point - but not earlier! - you just make your ORM print the queries, fix them manually (or write them from scratch). You then can ditch the ORM, use it as a query builder DSL only, or use escape hatches provided by the ORM to inject raw SQL. Don't use ORMs as a crutch to get away with not knowing SQL, that's bad. However, saving "a bit" - and with good ORMs, that "bit" is quite large - of time in the beginning is often very valuable. Just have a clear "exit strategy" for when (and in 90% of projects, if) it's needed.
- pid-1 3y agoSQL is like regex, one of those things I will keep unlearning if I'm not using daily. Also the way each DB supports its own dialer is quite maddening.
- vmfunction 3y agoSQL is a programming language! It is just declarative. And most programmers are associating imperative programming language as programming language. [1] I found while declarative and functional programming languages are not as often used, however learning them made me a better programer. [1]: https://stackoverflow.com/questions/10925689/functional-programming-vs-declarative-programming-vs-imperative-programming https://stackoverflow.com/questions/10925689/functional-prog...