Programming

SQL

What Is SQL?

SQL is a language used to query and change data in a relational database. A table is made of rows and columns. SQL selects rows from that table, adds a new row, updates an existing row, or deletes one.

SQL is not a general-purpose programming language like Python or JavaScript. Its job is not to build the program window. Its job is to talk to stored data. A site or an app often keeps its data in a database and reaches it with SQL.

Systems such as PostgreSQL, MySQL, SQLite, and SQL Server accept SQL. The main commands are similar, but each system can have small differences. A query that works in one system may not be written with the same letters in another.

What Is SQL Used For?

SQL is used where data is stored:

  • Selecting the rows you need
  • Adding a new record
  • Changing an existing record
  • Deleting a record that is no longer needed
  • Counting and grouping by a condition
  • Joining matching rows from more than one table

For example, when an application wants to show only one user's own records, that condition is usually written in the SQL query. If the condition is wrong, the result may be empty or larger than it should be.

What Can You Do with SQL?

With SQL you can do the basic work on a table:

  • Read all columns or only some of them
  • Find rows that match a condition
  • Write a new row
  • Change the value of a column
  • Delete a selected row

A simple example

SELECT name
FROM books
WHERE year = 2026;

This query selects only the names in the books table for the year 2026. The table and the columns must already exist.

Is SQL Difficult to Learn?

Reading a first SELECT query is not hard. It gets harder with conditions, more than one table, and a wrong delete command. A DELETE written without WHERE can remove every row in the table.

This order is useful at the start:

  1. What a table, a row, and a column are
  2. SELECT and FROM
  3. A condition with WHERE
  4. A new row with INSERT
  5. A change with UPDATE
  6. Trying DELETE on a row you selected first
  7. Changing the same query yourself on a small table

It is safer to write the first queries against an empty little table, not against a real project's database.

Advantages and Disadvantages

Advantages

  • It selects the needed rows without walking the whole table by hand
  • The main commands are similar in several database systems
  • A condition, an order, and a count can sit in the same query
  • Application code does not have to keep all the data inside itself
  • There are free database systems for learning

Disadvantages

  • It does not write a page, a button, or game logic
  • The spelling is not identical in every system
  • An update or a delete without a condition can change many rows
  • Pasting text a user typed straight into the query is unsafe

Syntax and Basic Concepts

A query usually starts with the command that says what to do. SELECT reads, INSERT adds, UPDATE changes, and DELETE removes. The table name comes after FROM or INTO.

Reading

SELECT name, year
FROM books
WHERE year >= 2020;

Adding

INSERT INTO books (name, year)
VALUES ('Sample book', 2026);

Changing

UPDATE books
SET year = 2025
WHERE name = 'Sample book';

The WHERE part says which rows are touched. Leaving it out of UPDATE or DELETE can change the rest of the table instead of one matching row.

Code Examples

Rows that match one condition

SELECT title
FROM notes
WHERE lang = 'en';

Counting

SELECT COUNT(*)
FROM notes
WHERE lang = 'en';

Deleting only the selected row

DELETE FROM notes
WHERE id = 15;

Working examples

The short queries above read one thing. These four build a small table and then use it. Run them in order on an empty database. SQLite is enough. The first makes the table. The second fills two rows. The third keeps one of them. The fourth changes a number.

Make the table

CREATE TABLE makes an empty table named students. id is a whole number and the primary key, so two rows cannot share the same id. name is text and NOT NULL, so a row without a name is refused. score is a whole number and may be empty. This line does not add Aysel or Murad. It only builds the columns.

CREATE TABLE students (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  score INTEGER
);

After this, the table exists and has zero rows. Run it a second time and the database says the table is already there.

Add two students

INSERT writes rows. The names in parentheses are the columns. The values follow in the same order: id, name, score. Aysel has 85. Murad has 40. A text value sits in quotes. A number does not. If you insert id 1 again, the primary key stops the second copy.

INSERT INTO students (id, name, score) VALUES
  (1, 'Aysel', 85),
  (2, 'Murad', 40);

The table now has two rows. Nothing is printed unless you ask with SELECT.

Keep only a passing score

SELECT chooses columns. WHERE throws away rows that fail the check. 85 stays, 40 does not, because 40 is below 50. ORDER BY score DESC puts the larger score first. DESC means descending. Without WHERE you would see both students. Without ORDER BY the database may return them in any order.

SELECT name, score
FROM students
WHERE score >= 50
ORDER BY score DESC;

One row comes back: Aysel, 85. Murad is still in the table. This query only hid him.

Change one score

UPDATE writes a new value into a column that already exists. SET says the new score is 70. WHERE says only the row named Murad changes. The SELECT under it reads the table again so you can see both rows. Forget the WHERE and every student's score becomes 70. That is the usual accident.

UPDATE students
SET score = 70
WHERE name = 'Murad';

SELECT name, score
FROM students;

Murad is now 70. Aysel stays 85. The old 40 is gone, because UPDATE replaces it.

Frequently Asked Questions

What is SQL?

SQL is a query language used to read and change data in a relational database.

Is SQL a programming language?

It is a query language for data. It is not used the way Python is used to write the whole application.

Is SQL free?

Learning the language is free. Systems such as PostgreSQL and SQLite can also be installed for free. Some server products have their own terms.

Are SQL and MySQL the same?

No. SQL is the language. MySQL is one database system that accepts that language.

Are Excel and SQL the same?

No. A table is a similar idea, but SQL works by query and is meant for rows stored in a database.

Can one wrong query delete data?

Yes. A DELETE or an UPDATE written without WHERE can touch more rows than you expected. Try it first on a small table.

Can text typed by a user be pasted straight into a query?

No. Instead of gluing that text into the query, use a separate parameter that the database accepts.

Tool for this topic