Code / SQL with Data / Unit 1: Tables and SELECT

Nicer results: AS and DISTINCT सुंदर नतीजे: AS और DISTINCT

Lesson 2 of 13 · about 10 minutes · Rename columns and remove repeated values.

⚡ 0 XP

Step 1 of 2

Rename with AS

itemqtyatta2dal1chai6poha3chai 6poha 3WHERE qty > 2

Column names in a database are often short or technical. AS gives a column a friendly name in the result (an alias):

SELECT name AS player, team AS city FROM players;

The table itself does not change — only the heading in your result. Aliases are very useful later, when you calculate new columns.

Example
SELECT name AS player,
       team AS city
FROM players;

🎯 Challenge: Scoreboard headings +10 XP

For all players, show name renamed to player and runs renamed to total_runs (in that order).

Try twice to unlock the solution (0/2)

query.sqlCtrl ⏎
Tables (practice database)
Results

Press Run to see your query’s rows here.

Checklist

Press Run to check your code.

  • Your rows match the expected answer
  • Uses AS
Lesson notes (text version)

1. Rename with AS

Column names in a database are often short or technical. AS gives a column a friendly name in the result (an alias):

SELECT name AS player, team AS city FROM players;

The table itself does not change — only the heading in your result. Aliases are very useful later, when you calculate new columns.

                SELECT name AS player,
       team AS city
FROM players;
              

Challenge: Scoreboard headings

For all players, show name renamed to player and runs renamed to total_runs (in that order).

2. Each value once: DISTINCT

SELECT team FROM players; prints a team once for every player, so Mumbai appears three times. To see each different value only once, add DISTINCT:

SELECT DISTINCT role FROM players;

This answers questions like “which teams are there?” or “which categories does the shop sell?”

                SELECT DISTINCT role FROM players;
              

Challenge: Which teams play?

Show the column team, with each team only once. (There are 5 teams.)

Search by task, format or tool name.