{"id":33,"date":"2021-03-19T12:12:48","date_gmt":"2021-03-19T12:12:48","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/?page_id=33"},"modified":"2021-03-22T11:40:20","modified_gmt":"2021-03-22T11:40:20","slug":"sql-select","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/national-5\/sql-select\/","title":{"rendered":"SQL SELECT (search)"},"content":{"rendered":"<p>The SELECT command is used to search database tables for specific records.<\/p>\n<p>There is always a SELECT and a FROM, but there may or not be a WHERE or ORDER BY<\/p>\n<p>The order is of these lines must be:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT columns\r\nFROM tables\r\nWHERE conditions\r\nORDER BY sorting\r\n<\/pre>\n<hr \/>\n<h1>SELECT &amp; FROM lines<\/h1>\n<p>Specify which fields are to be displayed, and which tables are to be used<\/p>\n<h2>Display all columns<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT *\r\nFROM orders <\/pre>\n<h2>Display some columns<\/h2>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT product, customer, quantity\r\nFROM orders\r\n<\/pre>\n<hr \/>\n<h1>WHERE conditions (optional)<\/h1>\n<p>This is used to select records from the tables. Conditions are described on the <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/reference\/conditions\/\">Conditions<\/a> reference page.<\/p>\n<p>If there is no WHERE line, then all records are displayed<\/p>\n<pre class=\"brush: sql; highlight: [3]; title: ; notranslate\" title=\"\">\r\nSELECT product, customer, quantity\r\nFROM orders\r\nWHERE quantity &gt; 100 <\/pre>\n<hr \/>\n<h1>ORDER BY sorting (optional)<\/h1>\n<p>Specify the sorting order of columns<\/p>\n<h2>Single Column sort, ascending order<\/h2>\n<pre class=\"brush: sql; highlight: [3]; title: ; notranslate\" title=\"\">\r\nSELECT product, customer, quantity \r\nFROM orders \r\nORDER BY quantity ASC<\/pre>\n<h2>Single Column sort, descending order<\/h2>\n<pre class=\"brush: sql; highlight: [3]; title: ; notranslate\" title=\"\">\r\nSELECT product, customer, quantity \r\nFROM orders \r\nORDER BY quantity DESC<\/pre>\n<h2>Two Column sort<\/h2>\n<pre class=\"brush: sql; highlight: [3]; title: ; notranslate\" title=\"\">\r\nSELECT product, customer, quantity \r\nFROM orders \r\nORDER BY quantity DESC, product ASC<\/pre>\n<hr \/>\n<h1>Queries across two tables<\/h1>\n<p>When a column name appears in both tables, the name of the table must be included (eg customers.customer_id). (line 1)<\/p>\n<p>The primary key and foreign keys of each pair of tables must be connected in the WHERE section (line 3)<\/p>\n<pre class=\"brush: sql; highlight: [1,3]; title: ; notranslate\" title=\"\"> \r\nSELECT forename, surname, customers.customer_id, product, quantity\r\nFROM orders, customers\r\nWHERE orders.customer_id = customers.customer_id<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The SELECT command is used to search database tables for specific records. There is always a SELECT and a FROM, but there may or not be a WHERE or ORDER BY The order is of these lines must be: SELECT columns FROM tables WHERE conditions ORDER BY sorting SELECT &amp; FROM lines Specify which fields are to be displayed, and which tables are to be used Display all columns SELECT<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/national-5\/sql-select\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":66,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-33","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":19,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/33\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/33\/revisions\/117"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/66"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/media?parent=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}