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

Vue3.0 handwriting magnifying glass effect

The effect to be achieved is: fixed zoom in twice...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

Programs to query port usage and clear port usage in Windows operating system

In Windows operating system, the program to query...

Install mysql 5.6 from yum source in centos7.4 system

System environment: centos7.4 1. Check whether th...

Mysql database index interview questions (basic programmer skills)

Table of contents introduction Indexing principle...

Importance of background color declaration when writing styles

As the title says, otherwise when the page is revi...

How to change the root password in MySQL 5.7

Starting from MySQL 5.7, many security updates ha...

Web Design Experience: 5 Excellent Web Design Concepts Full Analysis (Pictures)

Unlike other types of design, web design has been ...

Detailed explanation of flex layout in CSS

Flex layout is also called elastic layout. Any co...

How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Hexo binds a custom domain name to GitHub under W...

VUE implements bottom suction button

This article example shares the specific code of ...

Various methods to implement the prompt function of text box in html

You can use the attribute in HTML5 <input="...

Definition and function of zoom:1 attribute in CSS

Today I was asked what the zoom attribute in CSS ...

How to build Nginx image server with Docker

Preface In general development, images are upload...