{"id":3904,"date":"2025-06-06T20:00:50","date_gmt":"2025-06-06T20:00:50","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3904"},"modified":"2025-06-07T09:54:56","modified_gmt":"2025-06-07T09:54:56","slug":"sql-select","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-select\/","title":{"rendered":"SQL SELECT"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>SELECT<\/code> statement is how we <strong>read data<\/strong> from a table. Let\u2019s quickly go through the different ways to use it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Usage<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Select all columns\nSELECT * FROM employees;\n\n-- Select specific columns\nSELECT name, salary FROM employees;\n\n-- Filter rows using WHERE\nSELECT name FROM employees WHERE department = 'HR';\n\n-- Use comparison and logical operators\nSELECT * FROM employees WHERE salary &gt; 50000 AND department = 'IT';\n\n-- Sort results by column\nSELECT name, salary FROM employees ORDER BY salary DESC;\n\n-- Get unique values (no duplicates)\nSELECT DISTINCT department FROM employees;\n\n-- Limit number of rows (varies by DBMS)\n-- MySQL \/ PostgreSQL\nSELECT * FROM employees LIMIT 5;\n-- SQL Server\nSELECT TOP 5 * FROM employees;\n-- Oracle 12c+\nSELECT * FROM employees FETCH FIRST 5 ROWS ONLY;\n\n-- Rename columns in output\nSELECT name AS employee_name, salary AS monthly_salary FROM employees;\n\n-- Use expressions in SELECT\nSELECT name, salary * 12 AS annual_salary FROM employees;\n\n-- SELECT from multiple tables using JOIN (basic preview)\nSELECT e.name, d.name AS department_name\nFROM employees e\nJOIN departments d ON e.department_id = d.id;\n\n-- Create a new table from a SELECT\n-- SQL Server \/ PostgreSQL \/ Oracle\nSELECT * INTO temp_employees FROM employees WHERE department = 'Sales';\n-- MySQL alternative\nCREATE TABLE temp_employees AS\nSELECT * FROM employees WHERE department = 'Sales';<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SELECT<\/code> reads data<\/li>\n\n\n\n<li>Use <code>WHERE<\/code> to filter, <code>ORDER BY<\/code> to sort<\/li>\n\n\n\n<li><code>DISTINCT<\/code> removes duplicates<\/li>\n\n\n\n<li>Use aliases with <code>AS<\/code><\/li>\n\n\n\n<li>Add <code>LIMIT<\/code>, <code>TOP<\/code>, or <code>FETCH<\/code> to limit rows<\/li>\n\n\n\n<li>You can even create new tables using <code>SELECT INTO<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 That\u2019s it! Simple and powerful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SELECT statement is how we read data from a table. Let\u2019s quickly go through the different ways to use it. \ud83d\udd39 Basic Usage \ud83e\udde0 Quick Recap \u2705 That\u2019s it! Simple and powerful.<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3904","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3904","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=3904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}