Retrieving Data Cheat Sheet < Introduction to SQL

 


Retrieving Data

SELECT
clause is used to retrieve rows from a table.

Database The database consists of a

player
table that stores the details of players who are a part of a tournament.
player
table stores the name, age and score of players.

Let's explore more about the SELECT clause using the database!

Selecting Specific Columns

To retrieve the data of only specific columns from a table, add the respective column names in the SELECT clause.

Syntax

SELECT
column1,
column2,
...,
columnN
FROM
table_name;
SQL

Example

Let's fetch the

name
and
age
of the players from the
player
table.

SELECT
name,
age
FROM
player;
SQL
Output
nameage
Virat32
Rakesh39
Sai47
------

Selecting All Columns

Sometimes, we may want to select all the columns from a table. Typing out every column name, for every time we have to retrive the data, would be a pain. We have a shortcut for this!

Syntax

SELECT *
FROM table_name;
SQL

Example

Get all the data of players from the

player
table.

SELECT *
FROM player;
SQL
Output
nameagescore
Virat3250
Rakesh3935
Sai4730
---------

Selecting Specific Rows

We use WHERE clause to retrieve only specific rows.

Syntax

SELECT *
FROM table_name
WHERE condition;
SQL

WHERE
clause specifies a condition that has to be satisfied for retrieving the data from a database.

Example

Get

name
and
age
of the player whose
name
is "Ram" from the
player
table.

SELECT *
FROM player
WHERE name="Sai";
SQL
Output
nameagescore
Sai4730

Try it Yourself!!

The database consists of an

employee
table that stores the
employee_id
,
name
and
salary
of employees. Let's fetch data for the following queries.

  1. Get all the data from the
    employee
    table.
  2. Get
    name
    and
    salary
    of all the employees from the
    employee
    table.
  3. Get
    employee_id
    and
    salary
    whose
    name
    is "Raju" from the
    employee
    table.

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form