Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are differences between SQL queries?

Tags:

sql

postgresql

We've this query:

SELECT t FROM articles t WHERE t.article_id = 59446

Also known as:

SELECT articles FROM articles WHERE articles.article_id = 59446

I can not understand

SELECT articles FROM articles

What does this mean? Why it works?

Update: table 'articles' does not have column 'articles'

like image 670
Alexander Avatar asked Apr 16 '16 18:04

Alexander


People also ask

How do you find the difference between two queries in SQL?

By using UNION, UNION ALL, EXCEPT, and INTERSECT, we can compare two queries and get the necessary output as per our requirement. Given examples are simple to follow, we can have complex queries and can apply the mentioned constructs in them and can compare.

What is difference between SQL and query?

---- A SQL statement could be a query and a query could be a SQL statement... but, these are most definitely NOT the same or synonymous. A query can refer to any request for data based on various types of filtering criteria, projection, sorting, paging, size, etc instructions.

How do you compare two queries?

Comparing the Results of the Two Queries The solution to this is very simple. Run both queries using a UNION to combine the results! The UNION operator returns unique records. If the two results sets are identical the row count will remain the same as the original query.


1 Answers

This is a result of Postgres' object-relational architecture. For every table you create, there is also a matching composite type with the same name.

When you run

SELECT articles 
FROM articles

you are selecting a single column with the type articles from the table named articles. If you pay close attention to the output of that query you will notice that your result only contains a single column where the value is enclosed in parentheses, e.g. (1,Foobar) (if the table articles has two columns). If you run select * from articles the output is two columns (and no parentheses)

The same thing happens when you put the list of columns between parentheses:

select (article_id, article_name) 
from articles

also returns a single column with an anonymous composite type containing two fields (this is also a good example that "column" and "field" is not the same thing).

like image 55
a_horse_with_no_name Avatar answered Oct 06 '22 03:10

a_horse_with_no_name