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
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;
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)
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:
Sample Database: soccer
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
How to drop all tables from a database with one SQL query?
USE Databasename SELECT 'DROP TABLE [' + name + '];' FROM sys.tables
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
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