Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

SELECT - Basic Queries and Filtering Data in SQL


The SELECT statement is one of the most fundamental commands in SQL. It is used to query and retrieve data from one or more tables in a database. Using SELECT, you can specify which columns to retrieve, filter the data, and even perform calculations or join data from multiple tables.

1. Basic SELECT Query

A basic SELECT query is used to retrieve all columns from a table. By default, the query returns all rows in the table.

Example: Basic SELECT Query

      -- Select all columns from the 'Students' table
      SELECT * FROM Students;
    

In this example, the * symbol is used to select all columns from the 'Students' table. The result will include all rows and all columns in the table.

2. Selecting Specific Columns

You can also specify which columns you want to retrieve. Instead of using the * symbol, list the column names separated by commas.

Example: Selecting Specific Columns

      -- Select 'Name' and 'Age' columns from the 'Students' table
      SELECT Name, Age FROM Students;
    

This query will return only the 'Name' and 'Age' columns from the 'Students' table, omitting all other columns.

3. Filtering Data with WHERE

The WHERE clause is used to filter the rows returned by the SELECT statement. It allows you to specify conditions that the data must meet to be included in the result set.

Example: Filtering Data with WHERE

      -- Select students who are older than 18
      SELECT Name, Age FROM Students
      WHERE Age > 18;
    

In this example, the query will return only the rows where the 'Age' is greater than 18. The result will include the 'Name' and 'Age' columns of those students who meet this condition.

4. Using AND and OR for Multiple Conditions

You can use the AND and OR operators to combine multiple conditions in the WHERE clause. The AND operator requires that both conditions are true, while the OR operator requires that at least one condition is true.

Example: Using AND

      -- Select students who are older than 18 and have a grade above 80
      SELECT Name, Age, Grade FROM Students
      WHERE Age > 18 AND Grade > 80;
    

This query will return only the students who are older than 18 and have a grade greater than 80.

Example: Using OR

      -- Select students who are either older than 18 or have a grade above 80
      SELECT Name, Age, Grade FROM Students
      WHERE Age > 18 OR Grade > 80;
    

This query will return students who meet either of the conditions: they are older than 18 or have a grade above 80.

5. Sorting Data with ORDER BY

The ORDER BY clause is used to sort the results of a query. By default, it sorts the data in ascending order (from smallest to largest), but you can specify descending order as well.

Example: Sorting Data in Ascending Order

      -- Select students and sort them by 'Age' in ascending order
      SELECT Name, Age FROM Students
      ORDER BY Age ASC;
    

In this example, the data will be sorted by 'Age' in ascending order. The 'ASC' keyword is optional, as sorting is ascending by default.

Example: Sorting Data in Descending Order

      -- Select students and sort them by 'Grade' in descending order
      SELECT Name, Grade FROM Students
      ORDER BY Grade DESC;
    

This query will return the data sorted by 'Grade' in descending order, from the highest grade to the lowest.

6. Using LIMIT to Restrict the Number of Rows

The LIMIT clause allows you to restrict the number of rows returned by the query. This is useful when you only need a subset of the data, such as the top N records.

Example: Limiting the Number of Rows

      -- Select the top 5 students based on 'Grade'
      SELECT Name, Grade FROM Students
      ORDER BY Grade DESC
      LIMIT 5;
    

This query will return the top 5 students with the highest grades, sorted in descending order.

7. Using LIKE for Pattern Matching

The LIKE operator is used in the WHERE clause to search for a specified pattern in a column. It is often used with wildcard characters like % (representing any sequence of characters) and _ (representing a single character).

Example: Using LIKE for Pattern Matching

      -- Select students whose name starts with 'A'
      SELECT Name FROM Students
      WHERE Name LIKE 'A%';
    

This query will return students whose name begins with the letter 'A'. The % wildcard represents any characters after 'A'.

8. Using IN to Match Multiple Values

The IN operator allows you to specify multiple values in the WHERE clause. It is a shorthand for using multiple OR conditions.

Example: Using IN to Match Multiple Values

      -- Select students who are in either 'Math' or 'Science' courses
      SELECT Name, Course FROM Students
      WHERE Course IN ('Math', 'Science');
    

This query will return students who are enrolled in either 'Math' or 'Science' courses.

Conclusion

The SELECT statement is a powerful tool for querying and retrieving data from a database. By using clauses like WHERE, ORDER BY, LIMIT, LIKE, and IN, you can filter and sort data to meet your specific needs. Whether you're selecting all rows from a table or filtering based on complex conditions, understanding how to use the SELECT statement effectively is key to working with SQL databases.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java