Content

  • SQL insert into
  • SQL where clause
  • SQL and, or, not
  • SQL order by keyword
  • SQL null values

SQL insert into

The INSERT INTO statement is used to insert new records in a table.

It is possible to write the INSERT INTO statement in two ways.

Adding values for specific columns

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query.make sure the order of the values is in the same order as the columns in the table

INSERT INTO table_name VALUES (value1, value2, value3, ...);

SQL where clause

The WHERE clause is used to extract only those records that fulfill a specified condition.

SELECT column1, column2, ... FROM table_name WHERE condition;

WHERE clause is not only used in SELECT statement, it is also used in UPDATE, DELETE statement, etc.!


SQL and, or, not

The AND and OR operators are used to filter records based on more than one condition

SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ...;

The OR operator displays a record if any of the conditions separated by OR is TRUE

SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...;

The NOT operator displays a record if the condition(s) is NOT TRUE.

SELECT column1, column2, ... FROM table_name WHERE NOT condition;

SQL order by keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC/DESC;

SQL null values

If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.

SELECT column_names FROM table_name WHERE column_name IS NULL;
SELECT column_names FROM table_name WHERE column_name IS NOT NULL;