SQL proof of work

DealFlow SQL

A small PostgreSQL database for a commercial property deal pipeline, built to show working SQL rather than describe it. Ten queries, from a plain SELECT to window functions. Every table on this page is the real output of the query printed directly above it, captured from a live run.

The data is invented. Brokers, agencies, properties and values are synthetic seed data written for this project. No real company, person, client or transaction appears anywhere in it. Reproduce the whole thing with one command: ./run.sh

Q1  SELECT / WHERE / ORDER BY

Top open deals by value: the live pipeline, biggest first.

SELECT subject, status, deal_value_zar
FROM deals
WHERE status IN ('New', 'In review', 'Qualified')
  AND deal_value_zar > 0
ORDER BY deal_value_zar DESC;
subjectstatusdeal_value_zar
Century City mixed use blockNewR96,000,000
Kempton Park land rezoningIn reviewR55,000,000
Montague Gardens warehouse disposalIn reviewR42,000,000
Northgate anchor tenant saleIn reviewR41,200,000
Riverside Heights bulk saleIn reviewR33,800,000
Sandton Exchange floor 9NewR31,000,000
Paarden Eiland depot mandateQualifiedR27,500,000
Harbour sheds portfolioQualifiedR23,400,000
Foreshore sublease opportunityNewR18,500,000
Montague Park expansion erfNewR12,800,000
Harbour sheds tenant buyoutNewR9,800,000
Northgate parking leaseQualifiedR4,600,000

12 rows

Q2  WHERE with multiple conditions + date ordering

Review queue: flagged deals with their reasons, newest first.

SELECT subject, review_reason, deal_value_zar, received_at
FROM deals
WHERE needs_review = true
ORDER BY received_at DESC;
subjectreview_reasondeal_value_zarreceived_at
Foreshore naming rights queryNo deal value stated in mailR02026-07-17 13:27:00
Riverside Heights bulk saleDeal value above the review thresholdR33,800,0002026-07-12 15:08:00
Kempton Park land rezoningRezoning risk not yet verifiedR55,000,0002026-07-09 08:55:00
Northgate anchor tenant saleDeal value above the review thresholdR41,200,0002026-07-03 11:02:00

4 rows

Q3  INNER JOIN

Deals with the sector and province of the property they concern.

SELECT d.subject, p.sector, p.province, d.deal_value_zar
FROM deals d
JOIN properties p ON p.property_id = d.property_id
ORDER BY d.received_at;
subjectsectorprovincedeal_value_zar
Montague Gardens warehouse disposalIndustrialWestern CapeR42,000,000
Foreshore sublease opportunityOfficeWestern CapeR18,500,000
Northgate anchor tenant saleRetailGautengR41,200,000
Paarden Eiland depot mandateIndustrialWestern CapeR27,500,000
Sandton Exchange floor 9OfficeGautengR31,000,000
Ridge Plaza food court rightsRetailKwaZulu-NatalR8,200,000
Kempton Park land rezoningLandGautengR55,000,000
Century City mixed use blockMixed useWestern CapeR96,000,000
Harbour sheds portfolioIndustrialEastern CapeR23,400,000
Riverside Heights bulk saleResidentialGautengR33,800,000
Montague Park expansion erfIndustrialWestern CapeR12,800,000
Foreshore naming rights queryOfficeWestern CapeR0
Northgate parking leaseRetailGautengR4,600,000
Exchange Building auction noticeOfficeGautengR0
Harbour sheds tenant buyoutIndustrialEastern CapeR9,800,000

15 rows

Q4  Two INNER JOINs

Full deal sheet: who brought which deal on which property.

SELECT d.message_id, b.full_name AS broker, b.agency,
       p.common_name AS property, d.deal_value_zar, d.status
FROM deals d
JOIN brokers b ON b.broker_id = d.broker_id
JOIN properties p ON p.property_id = d.property_id
ORDER BY d.message_id;
message_idbrokeragencypropertydeal_value_zarstatus
MSG-0001Thandi MokoenaAtlantic PropertiesMontague Park WarehouseR42,000,000In review
MSG-0002Nolan FourieCape Commercial BrokersForeshore OfficesR18,500,000New
MSG-0003Sarah NaidooMetro Realty GroupNorthgate CentreR41,200,000In review
MSG-0004James BothaSouthpoint EstatesPaarden Eiland DepotR27,500,000Qualified
MSG-0005Lerato DlaminiUrban Yield CapitalExchange BuildingR31,000,000New
MSG-0006Sarah NaidooMetro Realty GroupRidge PlazaR8,200,000Declined
MSG-0007Nolan FourieCape Commercial BrokersKempton Land ParcelR55,000,000In review
MSG-0008Thandi MokoenaAtlantic PropertiesCentury City AnnexR96,000,000New
MSG-0009James BothaSouthpoint EstatesHarbour ShedsR23,400,000Qualified
MSG-0010Lerato DlaminiUrban Yield CapitalRiverside HeightsR33,800,000In review
MSG-0011Thandi MokoenaAtlantic PropertiesMontague Park WarehouseR12,800,000New
MSG-0012Nolan FourieCape Commercial BrokersForeshore OfficesR0New
MSG-0013Sarah NaidooMetro Realty GroupNorthgate CentreR4,600,000Qualified
MSG-0014Lerato DlaminiUrban Yield CapitalExchange BuildingR0Closed
MSG-0015James BothaSouthpoint EstatesHarbour ShedsR9,800,000New

15 rows

Q5  LEFT JOIN

Broker coverage: every broker, including those with zero deals.

SELECT b.full_name, b.agency, b.is_active,
       count(d.deal_id) AS deal_count,
       coalesce(sum(d.deal_value_zar), 0) AS total_value_zar
FROM brokers b
LEFT JOIN deals d ON d.broker_id = b.broker_id
GROUP BY b.broker_id, b.full_name, b.agency, b.is_active
ORDER BY deal_count DESC, b.full_name;
full_nameagencyis_activedeal_counttotal_value_zar
James BothaSouthpoint EstatesYes3R60,700,000
Lerato DlaminiUrban Yield CapitalYes3R64,800,000
Nolan FourieCape Commercial BrokersYes3R73,500,000
Sarah NaidooMetro Realty GroupYes3R54,000,000
Thandi MokoenaAtlantic PropertiesYes3R150,800,000
Michael AbrahamsPeninsula Property CoNo0R0

6 rows

Q6  GROUP BY (aggregate)

Open pipeline value by property sector.

SELECT p.sector,
       count(*) AS open_deals,
       sum(d.deal_value_zar) AS pipeline_zar,
       round(avg(d.deal_value_zar), 0) AS avg_deal_zar
FROM deals d
JOIN properties p ON p.property_id = d.property_id
WHERE d.status IN ('New', 'In review', 'Qualified')
GROUP BY p.sector
ORDER BY pipeline_zar DESC;
sectoropen_dealspipeline_zaravg_deal_zar
Industrial5R115,500,000R23,100,000
Mixed use1R96,000,000R96,000,000
Land1R55,000,000R55,000,000
Office3R49,500,000R16,500,000
Retail2R45,800,000R22,900,000
Residential1R33,800,000R33,800,000

6 rows

Q7  GROUP BY + HAVING

Agencies whose open pipeline exceeds R30M.

SELECT b.agency,
       count(*) AS open_deals,
       sum(d.deal_value_zar) AS pipeline_zar
FROM deals d
JOIN brokers b ON b.broker_id = d.broker_id
WHERE d.status IN ('New', 'In review', 'Qualified')
GROUP BY b.agency
HAVING sum(d.deal_value_zar) > 30000000
ORDER BY pipeline_zar DESC;
agencyopen_dealspipeline_zar
Atlantic Properties3R150,800,000
Cape Commercial Brokers3R73,500,000
Urban Yield Capital2R64,800,000
Southpoint Estates3R60,700,000
Metro Realty Group2R45,800,000

5 rows

Q8  GROUP BY on a time bucket (date_trunc)

Weekly deal flow: deals received and value per week. Same pattern with date_trunc('month', ...) gives monthly deal flow once the data spans more than one month.

SELECT date_trunc('week', received_at)::date AS week_starting,
       count(*) AS deals_received,
       sum(deal_value_zar) AS value_received_zar
FROM deals
GROUP BY week_starting
ORDER BY week_starting;
week_startingdeals_receivedvalue_received_zar
2026-06-294R129,200,000
2026-07-066R247,400,000
2026-07-133R17,400,000
2026-07-202R9,800,000

4 rows

Q9  Subquery (NOT EXISTS)

Vacancy-style view: properties with no open deal against them, i.e. stock on the books that nothing live is happening on.

SELECT p.common_name, p.sector, p.province
FROM properties p
WHERE NOT EXISTS (
  SELECT 1
  FROM deals d
  WHERE d.property_id = p.property_id
    AND d.status IN ('New', 'In review', 'Qualified')
)
ORDER BY p.sector, p.common_name;
common_namesectorprovince
Ridge PlazaRetailKwaZulu-Natal

1 row

Q10  Window functions (RANK + share of total)

Broker leaderboard: rank by open pipeline and share of the whole book.

SELECT rank() OVER (ORDER BY sum(d.deal_value_zar) DESC) AS rank,
       b.full_name,
       b.agency,
       sum(d.deal_value_zar) AS pipeline_zar,
       round(100.0 * sum(d.deal_value_zar)
             / sum(sum(d.deal_value_zar)) OVER (), 1) AS pct_of_book
FROM deals d
JOIN brokers b ON b.broker_id = d.broker_id
WHERE d.status IN ('New', 'In review', 'Qualified')
GROUP BY b.broker_id, b.full_name, b.agency
ORDER BY rank;
rankfull_nameagencypipeline_zarpct_of_book
1Thandi MokoenaAtlantic PropertiesR150,800,00038.1
2Nolan FourieCape Commercial BrokersR73,500,00018.6
3Lerato DlaminiUrban Yield CapitalR64,800,00016.4
4James BothaSouthpoint EstatesR60,700,00015.3
5Sarah NaidooMetro Realty GroupR45,800,00011.6

5 rows