Knowledgebase

Understanding SQL Query Basics Print

  • databasesphpmyadmin, databases, https, guide, howto, solution, zillionkinghost, hosting
  • 0

Enough to be useful.

SELECT

Reads data.

SELECT * FROM table_name LIMIT 10;

The asterisk means every column. LIMIT restricts how many rows come back, which is wise on a large table.

WHERE

Narrows which rows.

SELECT * FROM wp_options WHERE option_name = 'siteurl';

Without WHERE, you get everything.

UPDATE

Changes rows.

UPDATE wp_options SET option_value = 'https://example.com' WHERE option_name = 'siteurl';

The WHERE clause is what restricts it to one row. Without it, every row changes.

DELETE

Removes rows. Same warning about WHERE.

ORDER BY AND COUNT

ORDER BY sorts. COUNT(*) tells you how many rows match, which is useful before a change.

SELECT COUNT(*) FROM table WHERE condition;

Run that before any UPDATE or DELETE with the same condition. The number tells you what you are about to affect.

THE HABIT

SELECT first, always.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot