I am trying to come up with a SQL query string to read information from three tables, where they have overlapping column names:
Table: Task
Id
TaskDescription
...
Table: TaskAttempt
Id
TaskId
User
...
Table: TaskSubmission
Id
TaskAttemptId
Data
...
A task contains a description. Users can attempt to complete the task and they submit their results. Each task attempt can have multiple submissions.
When reviewing, I want to pull in all task submissions and display the description, user, and data; I also need to pull in all the Id fields so that I can update the rows upon review completion.
What would a SQL statement look like to capture this?
To deal with column names that are the same in different tables, you have to prefix the columns with the table that you are requesting the data from. This is a VERY basic shell of what your query would look like. The
JOINstatements need the corresponding columns, but you did not provide the schema on how they match up. However, this answers your basic question about columns with the same name.Prefixes simply give the SQL engine enough extra data to know how to handle ambiguities, and they also can provide much more expressive code to future readers.