The SELECT statement in SQL is used to query a database and retrieve data from one or more tables. Here are some examples of how you can use the SELECT command:

  1. Selecting All Columns from a Table:
   SELECT * FROM employees;
  1. Selecting Specific Columns from a Table:
   SELECT first_name, last_name, salary FROM employees;
  1. Aliasing Column Names:
   SELECT first_name AS "First Name", last_name AS "Last Name" FROM employees;
  1. Filtering Rows with WHERE Clause:
   SELECT * FROM products WHERE category = 'Electronics';
  1. Sorting Rows with ORDER BY:
   SELECT product_name, price FROM products ORDER BY price DESC;
  1. Limiting Rows with LIMIT:
   SELECT * FROM orders LIMIT 10;
  1. Using Aggregate Functions (e.g., SUM, AVG, COUNT):
   SELECT AVG(salary) AS average_salary FROM employees;
  1. Joining Tables:
   SELECT customers.customer_id, customers.first_name, orders.order_id
   FROM customers
   INNER JOIN orders ON customers.customer_id = orders.customer_id;
  1. Grouping Data with GROUP BY:
   SELECT department_id, AVG(salary) AS avg_salary
   FROM employees
   GROUP BY department_id;
  1. Conditional Logic with CASE Statement:
   SELECT customers.customer_id, customers.first_name, orders.order_id
   FROM customers
   INNER JOIN orders ON customers.customer_id = orders.customer_id;

These are just some basic examples, and there are many more advanced features and options available in the SELECT statement. The specifics might vary slightly depending on the database system you are using (e.g., MySQL, PostgreSQL, SQL Server, Oracle), but the general syntax remains similar.

Categories: SQL

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *