{"id":3896,"date":"2025-06-06T18:42:01","date_gmt":"2025-06-06T18:42:01","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3896"},"modified":"2025-06-10T18:36:43","modified_gmt":"2025-06-10T18:36:43","slug":"sql-syntax","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-syntax\/","title":{"rendered":"SQL Syntax"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To effectively write SQL queries and commands, understanding the basic syntax rules is essential. SQL syntax defines how you structure commands so the database can interpret them correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u270d\ufe0f Basic Structure of SQL Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A SQL statement usually follows this general format:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>COMMAND &#91;columns]\nFROM table_name\n&#91;WHERE condition]\n&#91;GROUP BY column]\n&#91;HAVING condition]\n&#91;ORDER BY column]\n&#91;LIMIT number];<\/code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>COMMAND<\/strong>: The action you want to perform (<code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, <code>DELETE<\/code>, etc.)<\/li>\n\n\n\n<li>Square brackets <code>[ ]<\/code> indicate optional parts of the statement.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u26a0\ufe0f Key SQL Syntax Rules<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>SQL Keywords are not case sensitive<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>select<\/code>, <code>SELECT<\/code>, and <code>Select<\/code> all work the same, but by convention, keywords are written in <strong>uppercase<\/strong> for clarity.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Statements end with a semicolon (<code>;<\/code>)<\/strong>\n<ul class=\"wp-block-list\">\n<li>This marks the end of a command.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>String values must be enclosed in single quotes<\/strong> sqlCopyEdit<code>WHERE name = 'John'<\/code><\/li>\n\n\n\n<li><strong>Identifiers (like table or column names) may require quotes if they contain spaces or special characters<\/strong>\n<ul class=\"wp-block-list\">\n<li>Typically backticks <code>`<\/code> in MySQL, double quotes <code>\"<\/code> in PostgreSQL.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Whitespace (spaces, tabs, new lines) is ignored<\/strong>\n<ul class=\"wp-block-list\">\n<li>Use whitespace to make code readable.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 Example: Simple SELECT Query Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT first_name, last_name\nFROM employees\nWHERE department = 'Sales'\nORDER BY last_name ASC;<\/code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Retrieves first and last names of employees in the Sales department, sorted by last name ascending.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f Common SQL Commands Syntax Overview<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Command<\/th><th>Basic Syntax<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td>SELECT<\/td><td><code>SELECT columns FROM table [WHERE condition];<\/code><\/td><td>Retrieve data<\/td><\/tr><tr><td>INSERT<\/td><td><code>INSERT INTO table (cols) VALUES (vals);<\/code><\/td><td>Add new rows<\/td><\/tr><tr><td>UPDATE<\/td><td><code>UPDATE table SET col = val [WHERE condition];<\/code><\/td><td>Modify existing data<\/td><\/tr><tr><td>DELETE<\/td><td><code>DELETE FROM table [WHERE condition];<\/code><\/td><td>Remove data<\/td><\/tr><tr><td>CREATE TABLE<\/td><td><code>CREATE TABLE table (col datatype, ...);<\/code><\/td><td>Define new table structure<\/td><\/tr><tr><td>ALTER TABLE<\/td><td><code>ALTER TABLE table ADD column datatype;<\/code><\/td><td>Modify table structure<\/td><\/tr><tr><td>DROP TABLE<\/td><td><code>DROP TABLE table;<\/code><\/td><td>Delete table<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 Tips for Writing SQL Code<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>uppercase<\/strong> for SQL keywords for readability.<\/li>\n\n\n\n<li>Always end statements with a <strong>semicolon<\/strong>.<\/li>\n\n\n\n<li>Write <strong>clear and meaningful identifiers<\/strong> for tables and columns.<\/li>\n\n\n\n<li>Format queries with indentation and line breaks to improve clarity.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Rule<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>Keywords uppercase<\/td><td>Makes queries easier to read<\/td><\/tr><tr><td>Semicolon at end<\/td><td>Marks end of SQL command<\/td><\/tr><tr><td>Strings in single quotes<\/td><td>Defines literal string values<\/td><\/tr><tr><td>Identifiers quoted if needed<\/td><td>When names have spaces or special chars<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>To effectively write SQL queries and commands, understanding the basic syntax rules is essential. SQL syntax defines how you structure commands so the database can interpret them correctly. \u270d\ufe0f Basic Structure of SQL Statements A SQL statement usually follows this general format: \u26a0\ufe0f Key SQL Syntax Rules \ud83e\udde9 Example: Simple SELECT Query Syntax \ud83d\udee0\ufe0f Common [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3896","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}