0
0
Expressframework~10 mins

Raw queries when needed in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Raw queries when needed
Receive HTTP request
Check if raw query needed?
NoUse ORM methods
Yes
Write raw SQL query string
Send query to database
Receive raw results
Send response to client
This flow shows how Express handles a request that needs a raw SQL query instead of ORM methods.
Execution Sample
Express
app.get('/users', async (req, res) => {
  const result = await db.query('SELECT * FROM users WHERE age > $1', [30]);
  res.json(result.rows);
});
This code runs a raw SQL query to get users older than 30 and sends the results as JSON.
Execution Table
StepActionQuery StringParametersResultResponse Sent
1Receive GET /users request
2Decide to run raw querySELECT * FROM users WHERE age > $1[30]
3Send query to databaseSELECT * FROM users WHERE age > $1[30]Rows with age > 30
4Receive query resultsRows with age > 30
5Send JSON responseRows with age > 30JSON array of users
💡 Response sent to client with raw query results
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
reqHTTP request objectHTTP request objectHTTP request objectHTTP request objectHTTP request object
queryStringSELECT * FROM users WHERE age > $1SELECT * FROM users WHERE age > $1SELECT * FROM users WHERE age > $1SELECT * FROM users WHERE age > $1
params[30][30][30][30]
resultRows with age > 30Rows with age > 30
resHTTP response objectHTTP response objectHTTP response objectHTTP response objectSent JSON response
Key Moments - 3 Insights
Why do we use raw queries instead of ORM methods sometimes?
Raw queries let us write custom SQL for complex or optimized operations that ORM methods can't easily do, as shown in step 2 of the execution_table.
How do parameters like [30] protect the query?
Parameters prevent SQL injection by separating data from code, as seen in the 'Parameters' column in steps 2 and 3.
What happens if the raw query returns no rows?
The result will be an empty array, and the response will send an empty JSON array, similar to step 5 but with no user data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the query string sent to the database at step 3?
ASELECT * FROM users WHERE age > $1
BSELECT * FROM users
CINSERT INTO users VALUES ($1)
DUPDATE users SET age = $1
💡 Hint
Check the 'Query String' column at step 3 in the execution_table.
At which step does the server send the JSON response back to the client?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look at the 'Response Sent' column in the execution_table.
If the parameter changed from [30] to [50], how would the result change?
AIt would return users younger than 30
BIt would return users older than 50 instead of 30
CIt would return all users regardless of age
DIt would cause a syntax error
💡 Hint
Refer to the 'Parameters' column and how it affects the query in the execution_table.
Concept Snapshot
Raw queries in Express let you write SQL directly for complex database tasks.
Use parameterized queries to keep data safe from injection.
Send raw SQL with db.query(sql, params) and handle results manually.
Useful when ORM methods are too limited or slow.
Always send the query results back as a response to the client.
Full Transcript
This visual execution shows how Express handles raw SQL queries when needed. First, the server receives a request. It decides to run a raw SQL query instead of using ORM methods. The raw SQL string and parameters are prepared. The query is sent to the database. The database returns matching rows. Finally, the server sends these rows as a JSON response to the client. Variables like the query string, parameters, and results change step by step. Beginners often wonder why raw queries are used, how parameters protect queries, and what happens if no data is returned. The quiz questions help check understanding of query strings, response timing, and parameter effects.