Conditionals can be used inside SUM() with group by aggregations
havingsum(name="XYZ") >0sum(casewhen x="something"then1else0end) as col_name
Use inverse
Instead of finding something using a complex query, use a subquery that does the opposite of the primary problem. Then use the outer query to find "not in subquery".
Reduce duplicate rows cross-join
WITH cte AS (SELECT BASKET_ID, SUB_COMMODITY_DESCFROM transaction_df tJOIN products_df pON t.PRODUCT_ID = p.PRODUCT_ID)SELECT lt.SUB_COMMODITY_DESC AS p1, rt.SUB_COMMODITY_DESC AS p2,COUNT(*) AS cntFROM cte ltJOIN cte rtON lt.BASKET_ID = rt.BASKET_IDAND lt.SUB_COMMODITY_DESC < rt.SUB_COMMODITY_DESC -- This lineAND lt.SUB_COMMODITY_DESC != rt.SUB_COMMODITY_DESCGROUP BY p1, p2ORDER BY cnt DESC
Interview Questions
Postgress SQL date diff
SELECT*FROM tablexyzWHEREDATEDIFF('2024-01-31', last_active_date) >30;-- using castSELECT*FROM tablexyzWHEREDATEDIFF(CAST('2024-01-31'ASdate), CAST(last_active_date ASdate)) >30;
**Use row number/rank in ascending and decending order to find first and last row
with cte as (select city , country , round(population/area) density , rank() over( order bypopulation/area) as rn , rank() over( order by (population/area) desc) as rn2from cities_population where area <>0)select city, country, density from cte where rn=1or rn2=1