SQL implementation of LeetCode (175. Joining two tables)

SQL implementation of LeetCode (175. Joining two tables)

[LeetCode] 175.Combine Two Tables

Table: Person

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.

Table: Address

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

FirstName, LastName, City, State

LeetCode also has a question about database. Let's do it. This is the first question. It is relatively simple. It is a question of joint search between two tables. We need to use Join operation. For some Join operations, you can read my previous blog SQL Left Join, Right Join, Inner Join, and Natural Join. The most direct way is to use Left Join to join the two tables according to PersonId:

Solution 1:

SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId;

When using Left Join, we can also use the Using keyword to declare which column name we want to use for the join:

Solution 2:

SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person LEFT JOIN Address USING(PersonId);

Or we can add the Natural keyword so that we don't have to declare specific columns and MySQL can search for the same columns by itself:

Solution 3:

SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person NATURAL LEFT JOIN Address;

References:

https://leetcode.com/discuss/21216/its-a-simple-question-of-left-join-my-solution-attached

https://leetcode.com/discuss/53001/comparative-solution-between-left-using-natural-left-join

This is the end of this article about SQL implementation of LeetCode (175. Join two tables). For more relevant SQL implementation of joining two tables, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL implementation of LeetCode (183. Customers who have never placed an order)
  • SQL implementation of LeetCode (182. Duplicate mailboxes)
  • SQL implementation of LeetCode (181. Employees earn more than managers)
  • SQL implements LeetCode (180. Continuous numbers)
  • SQL implementation of LeetCode (178. Score ranking)
  • SQL implementation of LeetCode (177. Nth highest salary)
  • SQL implementation LeetCode (176. Second highest salary)
  • SQL implementation of LeetCode (184. The highest salary in the department)

<<:  Specific use of Node.js package manager npm

>>:  HTML background color gradient achieved through CSS

Recommend

Two simple menu navigation bar examples

Menu bar example 1: Copy code The code is as foll...

MySQL should never write update statements like this

Table of contents Preface cause Phenomenon why? A...

Vue complete code to implement single sign-on control

Here is a Vue single sign-on demo for your refere...

The most comprehensive explanation of the locking mechanism in MySQL

Table of contents Preface Global Lock Full databa...

Introduction to JWT Verification Using Nginx and Lua

Table of contents Preface Lua Script nignx.conf c...

Example of using setInterval function in React

This article is based on the Windows 10 system en...

Solve the problem of Nginx returning 404 after configuring proxy_pass

Table of contents 1. Troubleshooting and locating...

Introduction to the functions and usage of value and name attributes in Html

1. The value used in the button refers to the text...

Steps for docker container exit error code

Sometimes some docker containers exit after a per...

Learn MySQL index pushdown in five minutes

Table of contents Preface What is index pushdown?...

A brief discussion on the magical slash in nginx reverse proxy

When configuring nginx reverse proxy, the slashes...

innodb_flush_method value method (example explanation)

Several typical values ​​of innodb_flush_method f...

Example of nginx ip blacklist dynamic ban

When a website is maliciously requested, blacklis...

What are HTML inline elements and block-level elements and their differences

I remember a question the interviewer asked durin...