SQL SELECT

SELECT Basics

SELECT first_name, last_name FROM employees;

This retrieves only the first_name and last_name columns.

Selecting All Columns

SELECT * FROM employees;

This returns every column in the employees table.

Using DISTINCT

SELECT DISTINCT department FROM employees;

Returns only unique department names.

Column Aliases

SELECT first_name AS name FROM employees;

Renames the first_name column in the output to name.

Filtering with WHERE

SELECT * FROM employees WHERE salary > 50000;
SELECT * FROM products WHERE name LIKE 'A%';

Only returns rows that match the condition.

Sorting with ORDER BY

SELECT * FROM employees ORDER BY salary DESC;

Sorts employees from highest to lowest salary.

Limiting Rows

SELECT * FROM employees LIMIT 5;

Returns only the first 5 rows.

Best Practices

  • List columns explicitly instead of using *
  • Use aliases for clarity, especially in joins
  • Filter results with WHERE to reduce load

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic