{"id":591,"date":"2016-04-17T07:05:05","date_gmt":"2016-04-17T07:05:05","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=591"},"modified":"2025-02-09T19:58:28","modified_gmt":"2025-02-10T02:58:28","slug":"sql-syntax","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-syntax\/","title":{"rendered":"SQL Syntax"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about SQL syntax that helps you to write SQL statements effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-sql-syntax'>Getting started with SQL syntax <a href=\"#getting-started-with-sql-syntax\" class=\"anchor\" id=\"getting-started-with-sql-syntax\" title=\"Anchor for Getting started with SQL syntax\">#<\/a><\/h2>\n\n\n\n<p>SQL (Structured Query Language) is the language you use to communicate with the databases.<\/p>\n\n\n\n<p>SQL is a <em>declarative<\/em> language. It means you tell the database <em>what<\/em> you want, not <em>how<\/em> to get it. This feature makes SQL very easy to learn compared to other programming languages.<\/p>\n\n\n\n<p>Basic SQL statement structures are verbs, subjects, and conditions.<\/p>\n\n\n\n<p>Most SQL statements follow a pattern:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Verb<\/strong> (Action): is the action you want the database to do, such as <code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, and <code>DELETE<\/code>.<\/li>\n\n\n\n<li><strong>Subject <\/strong>(Target): is the database object you work with, such as a table.<\/li>\n\n\n\n<li><strong>Condition<\/strong> (Filter): choose which data you&#8217;re interested in.<\/li>\n<\/ul>\n\n\n\n<p>For example, you want to find the employees hired in 1999. In SQL, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT first_name\nFROM employees\nWHERE YEAR (hire_date) = 1999;<\/code><\/span><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGZpcnN0X25hbWUsIGhpcmVfZGF0ZSBGUk9NIGVtcGxveWVlcyBXSEVSRSBZRUFSIChoaXJlX2RhdGUpID0gMTk5OTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SELECT first_name<\/code>: retrieves the values in the <code>first_name<\/code> column.<\/li>\n\n\n\n<li><code>FROM employees<\/code>: gets the data from the <code>employees<\/code> table.<\/li>\n\n\n\n<li><code>WHERE YEAR(hire_date) = 1999<\/code>: only consider employees who were joined in <code>1999<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-building-blocks'>SQL building blocks <a href=\"#sql-building-blocks\" class=\"anchor\" id=\"sql-building-blocks\" title=\"Anchor for SQL building blocks\">#<\/a><\/h2>\n\n\n\n<p>SQL statements consist of smaller pieces called tokens. These are like words and punctuation in an English sentence.<\/p>\n\n\n\n<p>Here are the important types of tokens:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='literals'>Literals <a href=\"#literals\" class=\"anchor\" id=\"literals\" title=\"Anchor for Literals\">#<\/a><\/h3>\n\n\n\n<p>Literals are the constants or actual values you place in SQL statement:<\/p>\n\n\n\n<p><strong>Strings<\/strong> are enclosed in single quotes like <code>'Anthony'<\/code> and <code>'Blue'<\/code>. Note that string literal is case-sensitive. For example, <code>'Anthony'<\/code> is different from <code>'anthony'<\/code>.<\/p>\n\n\n\n<p>If a string contains a single quote, you can escape it with another single quote like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-string\">'I'<\/span><span class=\"hljs-string\">'m Anthony'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>There are two single quote characters between the letters <code>I<\/code> and <code>m<\/code>.<\/p>\n\n\n\n<p><strong>Numbers<\/strong>. SQL supports integers, decimals, and floats. Literal numbers do not have quotes such as 100 and 59.99.<\/p>\n\n\n\n<p><strong>Booleans<\/strong>: are <code>true<\/code> and <code>false<\/code>.<\/p>\n\n\n\n<p><strong>Dates<\/strong>: date literals are in the format &#8216;yyyy-mm-dd&#8217;, for example <code>'2000-12-31'<\/code> is December 31, 2000.<\/p>\n\n\n\n<p><strong>Times<\/strong>: time literals are in the format <code>'hh:mm:ss'<\/code> such as <code>'12:45:30'<\/code> .<\/p>\n\n\n\n<p><strong>Timestamps<\/strong>: timestamps include both date and time. Timestamp literals use the format <code>'yyyy-mm-dd hh:mm:ss'<\/code> such as <code>'2000-12-31 12:45:30'<\/code> .<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='keywords'>Keywords <a href=\"#keywords\" class=\"anchor\" id=\"keywords\" title=\"Anchor for Keywords\">#<\/a><\/h3>\n\n\n\n<p>Keywords are reserved words in SQL. They have special meanings. The common keywords are <code>SELECT<\/code>, <code>FROM<\/code>, <code>WHERE<\/code>, <code>CREATE<\/code>, <code>INSERT<\/code>, etc.<\/p>\n\n\n\n<p>Keywords are case-insensitive. By convention, we&#8217;ll use uppercase for keywords to make them stand out.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='identifiers'>Identifiers <a href=\"#identifiers\" class=\"anchor\" id=\"identifiers\" title=\"Anchor for Identifiers\">#<\/a><\/h3>\n\n\n\n<p>Identifiers refer to objects in the database, such as tables, columns, indexes, etc.<\/p>\n\n\n\n<p><strong>Expressions (Combinations):<\/strong> Combinations of literals, identifiers, and operators that produce a value. For example, <code>salary * 1.10<\/code> (calculates a 10% raise) or <code>hire_date &lt; '2023-01-01'<\/code> (checks if the hire date is before January 1, 2023).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='comments'>Comments <a href=\"#comments\" class=\"anchor\" id=\"comments\" title=\"Anchor for Comments\">#<\/a><\/h3>\n\n\n\n<p>Comments are notes you add to your SQL code to explain why it does a specific task.<\/p>\n\n\n\n<p>The database ignores comments, but they&#8217;re helpful for you and others to understand and troubleshoot later.<\/p>\n\n\n\n<p>SQL supports two types of comments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single-line comments<\/strong> start with two hyphens (<code>--<\/code>) and is followed by a comment.<\/li>\n\n\n\n<li><strong>Multi-line comments<\/strong> start with <code>\/*<\/code> and end with <code>*\/<\/code>. The text between these is a comment.<\/li>\n<\/ul>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\">\/* \n   find employees who \n   earn more than $50,000 \n*\/<\/span>\n<span class=\"hljs-keyword\">SELECT<\/span> first_name  \n<span class=\"hljs-keyword\">FROM<\/span> employees\n<span class=\"hljs-keyword\">WHERE<\/span> salary &gt; <span class=\"hljs-number\">50000<\/span>; <span class=\"hljs-comment\">-- USD currency <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The best way to learn SQL is to practice it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SQL is declarative, i.e., you tell the database what you want, not how.<\/li>\n\n\n\n<li>Literals are constants you use in SQL statements. Literals are case-sensitive.<\/li>\n\n\n\n<li>Keywords are reserved words with special meanings. Keywords are case-insensitive.<\/li>\n\n\n\n<li>Identifiers are table names, columns, etc.<\/li>\n\n\n\n<li>Use a semicolon (;) to separate two SQL statements.<\/li>\n\n\n\n<li>Use a comment to add a note to SQL statements.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<p><iframe loading=\"lazy\" class=\"iframe\" src=\"\/quiz\/?quiz=syntax\" name=\"quiz\" width=\"600\" height=\"700\"><\/iframe><\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"591\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-syntax\/\"\n\t\t\t\tdata-post-title=\"SQL Syntax\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"591\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-syntax\/\"\n\t\t\t\tdata-post-title=\"SQL Syntax\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about SQL syntax that helps you to write SQL statements effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-591","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Syntax<\/title>\n<meta name=\"description\" content=\"This tutorial introduces you the SQL syntax: literals, keywords, identifiers &amp; expressions, predicates, clauses, comments.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqltutorial.org\/sql-syntax\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Syntax\" \/>\n<meta property=\"og:description\" content=\"This tutorial introduces you the SQL syntax: literals, keywords, identifiers &amp; expressions, predicates, clauses, comments.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-syntax\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-10T02:58:28+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-syntax\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-syntax\/\",\"name\":\"SQL Syntax\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"datePublished\":\"2016-04-17T07:05:05+00:00\",\"dateModified\":\"2025-02-10T02:58:28+00:00\",\"description\":\"This tutorial introduces you the SQL syntax: literals, keywords, identifiers & expressions, predicates, clauses, comments.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-syntax\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-syntax\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-syntax\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Syntax\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Syntax","description":"This tutorial introduces you the SQL syntax: literals, keywords, identifiers & expressions, predicates, clauses, comments.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqltutorial.org\/sql-syntax\/","og_locale":"en_US","og_type":"article","og_title":"SQL Syntax","og_description":"This tutorial introduces you the SQL syntax: literals, keywords, identifiers & expressions, predicates, clauses, comments.","og_url":"https:\/\/www.sqltutorial.org\/sql-syntax\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-10T02:58:28+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-syntax\/","url":"https:\/\/www.sqltutorial.org\/sql-syntax\/","name":"SQL Syntax","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"datePublished":"2016-04-17T07:05:05+00:00","dateModified":"2025-02-10T02:58:28+00:00","description":"This tutorial introduces you the SQL syntax: literals, keywords, identifiers & expressions, predicates, clauses, comments.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-syntax\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-syntax\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-syntax\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL Syntax"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=591"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/591\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}