0

Currently I am learning Python programming for manipulating SQLite database, and I am a bit confused with the following SQL statement which works.

SELECT * 
FROM student s, result r
WHERE s.studentid = r.studentid

When we give an alias to a table, we're taught to use the 'as' keyword before an alias, and the above SQL statement should be like this one:

SELECT * 
FROM student as s, result as r
WHERE s.studentid = r.studentid
  • Is the above SQL statement also correct?
  • What situation don't I need to add the keyboard 'as'?
  • Adding the keyword 'as' depends on our preferences? We can choose to add it or omit it randomly?
2
  • 10
    The as is optional. (But be consistent.) Commented Sep 25 at 11:45
  • 7
    Tip of today: Always use modern, explicit JOIN syntax. Easier to write (without errors), easier to read and maintain, and easier to convert to outer join if needed! Commented Sep 25 at 11:46

2 Answers 2

2

The ALIAS (AS) statement is used to provide a temporary name to a column of a table, aggregate function column or a table itself. A alias name exists for only for the duration of the query.

It is optional, but I would recommend using it.

Consider the following code (it might be a lot more columns)

SELECT column1 gender, column2 age, column3, column4

You might skip a/some comma/s and the following query would look like correct code as well but not the same as the one above

SELECT column1 gender, column2 age, column3 column4  -- note the missing comma between column3, column4

I have found on the official insert statement the usage of the alias, it might be described elsewhere as well, but it will be the same.


In your case those queries would be better written as:

SELECT s.*, r.* 
FROM student AS s
INNER JOIN result AS r ON s.studentid = r.studentid
Sign up to request clarification or add additional context in comments.

Comments

0

Both are correct, keyword 'as' is recommended to make the renaming and makes your query more readable

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.