Converting SQL to JavaScript API
Many common SQL queries can be written using the JavaScript API, provided by the SDK to wrap Data API calls. Below are a few examples of conversions between SQL and JavaScript patterns.
Select statement with basic clauses
Select a set of columns from a single table with where, order by, and limit clauses.
_10select first_name, last_name, team_id, age_10from players_10where age between 20 and 24 and team_id != 'STL'_10order by last_name, first_name desc_10limit 20;
_10const { data, error } = await supabase_10 .from('players')_10 .select('first_name,last_name,team_id,age')_10 .gte('age', 20)_10 .lte('age', 24)_10 .not('team_id', 'eq', 'STL')_10 .order('last_name', { ascending: true }) // or just .order('last_name')_10 .order('first_name', { ascending: false })_10 .limit(20)
Select statement with complex Boolean logic clause
Select all columns from a single table with a complex where clause: OR AND OR
_10select *_10from players_10where ((team_id = 'CHN' or team_id is null) and (age > 35 or age is null));
_10const { data, error } = await supabase_10 .from('players')_10 .select() // or .select('*')_10 .or('team_id.eq.CHN,team_id.is.null')_10 .or('age.gt.35,age.is.null') // additional filters imply "AND"
Select all columns from a single table with a complex where clause: AND OR AND
_10select *_10from players_10where ((team_id = 'CHN' and age > 35) or (team_id != 'CHN' and age is not null));
_10const { data, error } = await supabase_10 .from('players')_10 .select() // or .select('*')_10 .or('and(team_id.eq.CHN,age.gt.35),and(team_id.neq.CHN,.not.age.is.null)')