Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for SQL for newbies (as a web developer)
Damien Le Dantec
Damien Le Dantec

Posted on

     

SQL for newbies (as a web developer)

Context

Some time ago, as web developer, I had to use PostgreSQL and write some SQL queries to fetch some data.
I have a school level about SQL, so the first queries were difficult to write and I didn't find a memo about my use cases.

So this article acts as a "SQL memo" for web developer wondering to create basic SQL queries to fetch some data from SQL databases (especially PostgreSQL database, in my case).

Select data

The most basic query is the following:

select*frommy_table
Enter fullscreen modeExit fullscreen mode

Select all data from tablemy_table

We avoid fetching all data, we select the desired columns:

selectt.id,t.namefrommy_tablet
Enter fullscreen modeExit fullscreen mode

Select the columnsid andname frommy_table usingt as an alias for my table

It's common for us to filter data

selectt.id,t.namefrommy_tabletwheret.category='my_category'
Enter fullscreen modeExit fullscreen mode

Filter on thecategory

Renaming columns

To skip a mapping step during the fetch process, we can rename columns to match with our code base:

selectt.idas"idTable",t.nameas"nameTable"frommy_tablet
Enter fullscreen modeExit fullscreen mode

Rename the fieldsid andname asidTable andnameTable

Join the tables

Usually, we need to fetch data from multiple tables.
This section is not aSQL join course so keep in mind 2 things:

  • When you join 2 tables andnot want lose rows if some match doesn't exist on the second table, use aleft join
  • When you join 2 tables andwant to filter rows doesn't have a match with second table, useinner join

Tip : When you build your query, start byleft join and refine withinner join if necessary to avoid lose data

SQL joins
Credit

Left join

selectt.id,t2.addressfrommy_tabletleftjoinmy_table_2t2ont.id=t2.table_id
Enter fullscreen modeExit fullscreen mode

We join the tables using thetable_id column ofmy_table_2 that matches with theid column ofmy_table.

Usingleft join, no rows ofmy_table are lost but some fields ofmy_table_2 can benull.

Inner join

selectt.id,t2.addressfrommy_tabletinnerjoinmy_table_2t2ont.id=t2.table_id
Enter fullscreen modeExit fullscreen mode

We join the tables using thetable_id column ofmy_table_2 that matches with theid column ofmy_table

Usinginner join, some rows ofmy_table can be lost.

Build JSON object

As web developer, we like to use JSON objects. We can create the JSON using SQL to simplify the data manipulation.
Combined with theas keyword to rename columns, it is very useful!

selectjson_build_object('id',t.id,'name',t.name)asmy_json_objectfromm_tablet
Enter fullscreen modeExit fullscreen mode

On each row, we have the columnmy_json_object that contains JSON with the keys "id" and "name"

Create JSON array

Sometimes, we need to have arrays of JSON object. We can easily do it with SQL withjson_agg!

selectjson_agg(json_build_object('id',t.id,'name',t.name)asmy_json_object)asmy_json_arrayfromm_tablet
Enter fullscreen modeExit fullscreen mode

In the first (and only) row, in the columnmy_json_array, we have the wanted JSON array

Use "sub queries"

For complex queries, I can be useful to split big request into multiple one.
For example, a case of a query that aggregates multiple tables and another query based on the first one to do some logic to it.

selectjson_agg(json_build_object('id',"source".id,'address',"source".address))from(selectt.id,t2.addressfrommy_tabletleftjoinmy_table_2t2ont.id=t2.table_id)as"source"limit5
Enter fullscreen modeExit fullscreen mode

A "first select" fetch the data under "source" and the upper select format the data

Conclusion

This article is a smallmemo with only the most basic commands used in my use-cases in web development.
I hope it can help someone !

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web developer
  • Location
    Rennes, France
  • Work
    Zenika Rennes
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp