SQL Selecting All Data

Syntax:

Let's see the syntax below:

                                  
                                    SELECT * FROM table_name;
                                  
                                

Here, * is a wildcard character that represents all columns in the table. Replace table_name with the name of the table from which you want to retrieve all data.


Example:

Assuming you have a table named employees::

                                  
                                    CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    department VARCHAR(50),
    salary DECIMAL(10, 2)
);

INSERT INTO employees VALUES
    (1, 'John', 'Doe', 'HR', 50000.00),
    (2, 'Jane', 'Smith', 'IT', 60000.00),
    (3, 'Bob', 'Johnson', 'Finance', 55000.00);
                                  
                                

To select all data from the employees table:

                                
                                    SELECT * FROM employees;
                                
                              

This query will return all rows and columns from the employees table:

OUTPUT:

Employee ID First Name Last Name Department Salary
1 John Doe HR 50000.0
2 Jane Smith IT 60000.0
3 Bob Johnson Finance 55000.0

Tips:

  • Be cautious when using SELECT * in production code, especially in large tables. It might retrieve more data than needed, impacting performance.
  • If you only need specific columns, it's a good practice to explicitly list them in the SELECT statement rather than using *. This can improve query efficiency.
  • Use SELECT * during development or exploration to quickly view the structure and content of a table.