Skip to content

SQL, how to use SELECT

How to get data from a SQL database table

You can get data out of tables using the SELECT command.

Get all rows and columns:

SELECT * FROM people;
 age |  name  
-----+--------
  37 | Flavio
   8 | Roger

Get only the name column:

SELECT name FROM people;
  name  
--------
 Flavio
 Roger

Count the items in the table:

SELECT COUNT(*) from people;
 count 
-------
     2

You can filter rows in a table adding the WHERE clause:

SELECT age FROM people WHERE name='Flavio';
 age 
-----
  37

The results of a query can be ordered by column value, ascending (the default) or descending, using ORDER BY:

SELECT * FROM people ORDER BY name;
SELECT * FROM people ORDER BY name DESC;

I wrote 21 books to help you become a better developer:

  • HTML Handbook
  • Next.js Pages Router Handbook
  • Alpine.js Handbook
  • HTMX Handbook
  • TypeScript Handbook
  • React Handbook
  • SQL Handbook
  • Git Cheat Sheet
  • Laravel Handbook
  • Express Handbook
  • Swift Handbook
  • Go Handbook
  • PHP Handbook
  • Python Handbook
  • Linux Commands Handbook
  • C Handbook
  • JavaScript Handbook
  • Svelte Handbook
  • CSS Handbook
  • Node.js Handbook
  • Vue Handbook
...download them all now!

Related posts that talk about database: