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

Selecting Columns and Rows in SQL


In SQL, the SELECT statement is used to query and retrieve data from a database. One of the most fundamental operations in SQL is selecting columns and rows from a table. The SELECT statement allows you to specify which columns you want to retrieve and filter the rows based on certain conditions.

1. Basic SELECT Statement

The basic syntax of the SELECT statement involves specifying the columns you want to retrieve from a table. If you want to retrieve all columns, you can use the * wildcard.

Syntax:

      SELECT column1, column2, ...
      FROM table_name;
    

Example: Selecting Specific Columns

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

In this example, the query retrieves the 'Name' and 'Age' columns from the 'Students' table. Only the specified columns are returned, and all rows are included unless filtered otherwise.

Example: Selecting All Columns

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

In this example, the * wildcard is used to select all columns from the 'Students' table. This retrieves every column for every row in the table.

2. Selecting Rows with Conditions

You can use the WHERE clause to filter rows and retrieve only those that meet specific conditions. The WHERE clause is a powerful tool for narrowing down the results of a SELECT statement.

Syntax:

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

Example: Selecting Rows with a Condition

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

This query retrieves the 'Name' and 'Age' of students who are older than 18. The WHERE clause ensures that only rows where the 'Age' is greater than 18 are returned.

Example: Selecting Rows with Multiple Conditions

      -- Select students who are older than 18 and have a GPA greater than 3.5
      SELECT Name, Age, GPA
      FROM Students
      WHERE Age > 18 AND GPA > 3.5;
    

In this example, both conditions must be true for a row to be included in the result. The query retrieves students who are older than 18 and have a GPA greater than 3.5.

3. Using Logical Operators

SQL provides several logical operators that can be used in the WHERE clause to combine multiple conditions. The most commonly used logical operators are AND, OR, and NOT.

Example: Using the OR Operator

      -- Select students who are either older than 18 or have a GPA greater than 3.5
      SELECT Name, Age, GPA
      FROM Students
      WHERE Age > 18 OR GPA > 3.5;
    

In this case, the query retrieves students who meet either of the two conditions: being older than 18 or having a GPA greater than 3.5.

Example: Using the NOT Operator

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

This query returns students who are not older than 18 by using the NOT operator to negate the condition.

4. Sorting the Results with ORDER BY

You can use the ORDER BY clause to sort the results of a SELECT statement. By default, the ORDER BY clause sorts the results in ascending order (from lowest to highest). You can use the DESC keyword to sort in descending order.

Syntax:

      SELECT column1, column2, ...
      FROM table_name
      ORDER BY column1 [ASC|DESC];
    

Example: Sorting in Ascending Order

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

This query retrieves all students and sorts them by the 'Age' column in ascending order (youngest to oldest).

Example: Sorting in Descending Order

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

This query retrieves all students and sorts them by the 'GPA' column in descending order (highest to lowest).

5. Limiting the Number of Rows

In some cases, you may want to retrieve only a limited number of rows from the result set. SQL provides the LIMIT or TOP clause (depending on the database system) to restrict the number of rows returned.

Example: Using LIMIT (MySQL, PostgreSQL)

      -- Select the first 5 students from the 'Students' table
      SELECT Name, Age
      FROM Students
      LIMIT 5;
    

This query retrieves only the first 5 rows from the 'Students' table.

Example: Using TOP (SQL Server)

      -- Select the top 5 students from the 'Students' table
      SELECT TOP 5 Name, Age
      FROM Students;
    

In SQL Server, the TOP keyword is used to limit the number of rows returned.

6. Selecting Distinct Values

If you want to retrieve only unique (distinct) values from a column, you can use the DISTINCT keyword in your SELECT statement. This will remove duplicate values from the result set.

Example: Selecting Distinct Values

      -- Select distinct ages from the 'Students' table
      SELECT DISTINCT Age
      FROM Students;
    

This query retrieves only the distinct ages from the 'Students' table, eliminating any duplicate values.

7. Conclusion

The SELECT statement is one of the most commonly used SQL commands for querying data. By understanding how to select specific columns, filter rows using conditions, sort results, limit the number of rows, and eliminate duplicates, you can effectively extract the data you need from your 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