﻿ SQL: Find the position of the player who scored an own goal

# SQL Exercise: Find the position of the player who scored an own goal

## SQL soccer Database: Joins Exercise-58 with Solution

58. From the following table, write a SQL query to find out which players scored against his own team by accident. Return player name, jersey number, country name, age, position to play, and playing club.

Sample table: goal_details

Sample table: player_mast

Sample table: soccer_country

Sample Solution:

SQL Code:

``````SELECT player_name,
jersey_no,
country_name,
age,
posi_to_play,
playing_club
FROM goal_details a
JOIN player_mast b ON a.player_id=b.player_id
JOIN soccer_country c ON a.team_id=c.country_id
WHERE goal_type='O'
ORDER BY player_name;
```
```

Sample Output:

```    player_name     | jersey_no |    country_name     | age | posi_to_play | playing_club
--------------------+-----------+---------------------+-----+--------------+--------------
Birkir Saevarsson  |         2 | Iceland             |  31 | DF           | Hammarby
Ciaran Clark       |         3 | Republic of Ireland |  26 | DF           | Aston Villa
Gareth McAuley     |         4 | Northern Ireland    |  36 | DF           | West Brom
(3 rows)
```

Code Explanation:

The said query in SQL that selects a list of players who have scored own goals, along with their relevant information such as their jersey number, country, age, position, and playing club.
The JOIN clause combines the goal_details and player_mast tables are based on the player_id column, and the goal_details and soccer_country tables are joined based on the team_id and country_id columns.
The WHERE clause limits the results to only include goals that are of type 'O', which means own goals.
The results are sorted by player name in alphabetical order.

Relational Algebra Expression:

Relational Algebra Tree:

## Practice Online

Sample Database: soccer

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Find the defender who scored goal for his team.
Next SQL Exercise: Find the results of penalty shootout matches.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## SQL: Tips of the Day

What is the best way to paginate results in SQL Server?

```SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, *
FROM      Orders
WHERE     OrderDate >= '1980-01-01'
) AS RowConstrainedResult
WHERE   RowNum >= 1
AND RowNum < 20
ORDER BY RowNum
```

Database: SQL Server

Ref: https://bit.ly/3MGrNlk

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook