Editing
SQL
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == Common patterns: ; Basic retrieval :<code>SELECT name, email FROM users WHERE active = 1 ORDER BY created_at DESC LIMIT 10;</code> ; Joining tables :<code>SELECT orders.id, users.name, orders.total FROM orders JOIN users ON orders.user_id = users.id WHERE orders.status = 'shipped';</code> ; Aggregation :<code>SELECT product_id, COUNT(*) AS num_orders, SUM(total) AS revenue FROM orders GROUP BY product_id HAVING SUM(total) > 1000 ORDER BY revenue DESC;</code> ; Subquery :<code>SELECT name FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > 500);</code> ; Upsert (insert or update) :<code>INSERT INTO settings (user_id, key, value) VALUES (42, 'theme', 'dark') ON CONFLICT (user_id, key) DO UPDATE SET value = EXCLUDED.value;</code> ; Common Table Expression (CTE) :<code>WITH recent_orders AS ( SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '30 days' ) SELECT user_id, COUNT(*) FROM recent_orders GROUP BY user_id;</code> Practical habits: always filter early (move conditions as close to the source as possible), use EXPLAIN to inspect the query plan before deploying, and never run UPDATE or DELETE without a WHERE clause in production. </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information