I always forget how to do this for no apparent reason:
SELECT row1, row2, count(*) AS instances FROM table GROUP BY row1, row2 HAVING count(*) > 1;
The sad part is that I do it on a pretty frequent basis…
I always forget how to do this for no apparent reason:
SELECT row1, row2, count(*) AS instances FROM table GROUP BY row1, row2 HAVING count(*) > 1;
The sad part is that I do it on a pretty frequent basis…
→ Leave a CommentCategories: Uncategorized
I know this is rather easy to find, but writing it down helps me remember it:
This is extremely useful to keep in mind when you are writing queries. It can help you keep things a bit more optimized and it is nice to know where things actually start and end. It is also very useful to view how sql breaks down your query and goes about processing it.
Along these same lines it is useful to know what happens to a query once you hit enter. Here is what it does in PostgreSQL:
I am sure I will be updating with more DB things soon. There is much more detail here on postgres internals.
→ Leave a CommentCategories: Uncategorized
I have been working on sql much more than I care to lately and I ran across a problem. I had a table that had 2 columns referencing the same table and I needed to do a join on each column seperately.
The solution:
select t1.id1, t2a.cell, t1.id2, t2b.cell
from table1 as t1
inner join table2 as t2a on t1.id1 = t2a.id
inner join table2 as t2b on t1.id2 = t2b.id;
Fairly simple after all…
→ Leave a CommentCategories: Uncategorized