SQL basic queries

SQL queries are used to retrieve data from databases. Here are some basic SQL queries with examples:

Example:

Consider a table named "customers" with the following data:


  1. Now, let's use the basic SQL queries to retrieve data from this table:

  1. SELECT:

The following query retrieves all data from the "customers" table:

SELECT * FROM customers.

Result:


2.               WHERE:

The following query retrieves all data from the "customers" table where the age is greater than 25:

SELECT * FROM customers WHERE age > 25.

Here is the result:


This query filters the table to only display rows where the age column value is greater than 25, showing the details of customers who are older than 25 years old.

3.               ORDER BY:

The following query retrieves all data from the "customers" table and sorts the result set in descending order of age:

SELECT * FROM customers ORDER BY age DESC.

Here is the result:


This query sorts the entire table in a descending order based on the 'age' column, displaying the details of customers starting from the oldest to the youngest.

4.               DISTINCT:

The following query retrieves all distinct ages from the "customers" table:

SELECT DISTINCT age FROM customers.

Here is the result:


This query filters out duplicate values and presents a list containing only unique age values found within the 'age' column of the "customers" table.

5.               LIMIT:

The following query retrieves the first two rows from the "customers" table:

SELECT * FROM customers LIMIT 2.

Here is the result:


This query specifically limits the output to just the initial two rows from the "customers" table, showcasing the details of the first two entries found in the table.

Post a Comment

0 Comments