sqlouterjoin
Perform outer join on two database tables
Syntax
Description
returns a table created by performing an outer join on the specified left and right
database tables. This function matches rows using all shared columns, or keys, in
both tables and retains only rows that exist in both. This operation is equivalent
to executing the SQL statement data = sqlouterjoin(conn,lefttable,righttable)SELECT * FROM lefttable,righttable OUTER
JOIN lefttable.key = righttable.key.
conn can be any of the following connection objects:
MySQL®
PostgreSQL®
DuckDB™
ODBC
JDBC
Use this function to join two tables and include rows that do not have matching keys in one or both tables.
uses additional options specified by one or more name-value arguments. For example,
set data = sqlouterjoin(conn,lefttable,righttable,Name=Value)Keys="productNumber" to join the tables using
productNumber as the join key.
Examples
Use a MySQL® native interface database connection to import product data from an outer join between two MySQL database tables into MATLAB®.
Create a MySQL native interface database connection to a MySQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.
datasource = "MySQLNative"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);
Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Use the 'Type' name-value pair argument to retrieve records that have matching values in the selected column of both tables, and unmatched records from the left table only. data is a table that contains the matched and unmatched rows from the two tables.
lefttable = "productTable"; righttable = "suppliers"; data = sqlouterjoin(conn,lefttable,righttable,'Type',"left");
Display the first three rows of joined data. The columns from the right table (suppliers) appear to the right of the columns from the left table (productTable).
head(data,3)
ans=3×10 table
8 212569 1001 5 "Train Set" 1001 "Wonder Products" "New York" "United States" "212 435 1617"
1 400345 1001 14 "Building Blocks" 1001 "Wonder Products" "New York" "United States" "212 435 1617"
2 400314 1002 9 "Painting Set" 1002 "Terrific Toys" "London" "United Kingdom" "44 456 9345"
Close the database connection.
close(conn)
Use a PostgreSQL native interface database connection to import product data from an outer join between two PostgreSQL database tables into MATLAB®.
Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password. The database contains the tables productTable and suppliers.
datasource = "PostgreSQLDataSource"; username = "dbdev"; password = "matlab"; conn = postgresql(datasource,username,password);
Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. The sqlouterjoin function automatically detects the shared column between the tables. data is a table that contains the matched and unmatched rows from the two tables.
lefttable = "productTable"; righttable = "suppliers"; data = sqlouterjoin(conn,lefttable,righttable);
Display the first three rows of joined data. The columns from the right table (suppliers) appear to the right of the columns from the left table (productTable).
head(data,3)
ans=3×10 table
1 400345 1001 14 "Building Blocks" 1001 "Wonder Products" "New York" "United States" "212 435 1617"
2 400314 1002 9 "Painting Set" 1002 "Terrific Toys" "London" "United Kingdom" "44 456 9345"
3 400999 1009 17 "Slinky" 1009 "Doll's Galore" "London" "United Kingdom" "44 222 2397"
Close the database connection.
close(conn)
Use the sqlouterjoin function to perform an outer join of two DuckDB database tables.
Create a DuckDB database connection by using the duckdb function.
conn = duckdb;
Create a table to store product data by using the table function.
ProductName = ["ProductA"; "ProductB"; "ProductC"; "ProductD"]; ProductID = [101; 102; 103; 104]; ProductTable = table(ProductName, ProductID)
ProductTable=4×2 table
ProductName ProductID
___________ _________
"ProductA" 101
"ProductB" 102
"ProductC" 103
"ProductD" 104
Next, create a table to store supplier data.
SupplierID = [2001; 2003]; ProductID = [101; 103]; SupplierTable = table(SupplierID, ProductID)
SupplierTable=2×2 table
SupplierID ProductID
__________ _________
2001 101
2003 103
Export the tables to the DuckDB database by using the sqlwrite function.
productTableName = "productTable"; supplierTableName = "supplierTable"; sqlwrite(conn,productTableName,ProductTable); sqlwrite(conn,supplierTableName,SupplierTable);
Join the tables by using the sqlouterjoin function.
sqlouterjoin(conn,productTableName,supplierTableName)
ans=4×4 table
ProductName ProductID SupplierID ProductID_1
___________ _________ __________ ___________
"ProductA" 101 2001 101
"ProductB" 102 NaN NaN
"ProductC" 103 2003 103
"ProductD" 104 NaN NaN
Use an ODBC connection to import product data from an outer join between two Microsoft® SQL Server® database tables into MATLAB®. Specify the database catalog and schema where the tables are stored.
Create an ODBC database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password. The database contains the tables productTable and suppliers.
datasource = 'MS SQL Server Auth'; conn = database(datasource,'','');
Check the database connection. If the Message property is empty, then the connection is successful.
conn.Message
ans =
[]
Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Specify the toy_store catalog and the dbo schema for both the left and right tables. Use the 'LeftCatalog' and 'LeftSchema' name-value pair arguments for the left table, and the 'RightCatalog' and 'RightSchema' name-value pair arguments for the right table.
data is a table that contains the matched and unmatched rows from the two tables.
lefttable = 'productTable'; righttable = 'suppliers'; data = sqlouterjoin(conn,lefttable,righttable,'LeftCatalog','toy_store', ... 'LeftSchema','dbo','RightCatalog','toy_store','RightSchema','dbo');
Display the first three rows of joined data. The columns from the right table appear to the right of the columns from the left table.
head(data,3)
ans =
3×10 table
productNumber stockNumber supplierNumber unitCost productDescription SupplierNumber SupplierName City Country FaxNumber
_____________ ___________ ______________ ________ __________________ ______________ _________________ __________ ________________ ______________
1 4.0035e+05 1001 14 'Building Blocks' 1001 'Wonder Products' 'New York' 'United States' '212 435 1617'
2 4.0031e+05 1002 9 'Painting Set' 1002 'Terrific Toys' 'London' 'United Kingdom' '44 456 9345'
3 4.01e+05 1009 17 'Slinky' 1009 'Doll's Galore' 'London' 'United Kingdom' '44 222 2397'
Close the database connection.
close(conn)
Use an ODBC connection to import joined product data from two Microsoft® SQL Server® database tables into MATLAB®. Specify the key to use for joining the tables.
Create an ODBC database connection to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password. The database contains the tables productTable and suppliers.
datasource = 'MS SQL Server Auth'; conn = database(datasource,'','');
Check the database connection. If the Message property is empty, then the connection is successful.
conn.Message
ans =
[]
Join two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. Specify the key, or shared column, between the tables using the 'Keys' name-value pair argument.
data is a table that contains the matched and unmatched rows from the two tables.
lefttable = 'productTable'; righttable = 'suppliers'; data = sqlouterjoin(conn,lefttable,righttable,'Keys','supplierNumber');
Display the first three rows of joined data. The columns from the right table appear to the right of the columns from the left table.
head(data,3)
ans =
3×10 table
productNumber stockNumber supplierNumber unitCost productDescription SupplierNumber SupplierName City Country FaxNumber
_____________ ___________ ______________ ________ __________________ ______________ _________________ __________ ________________ ______________
1 4.0035e+05 1001 14 'Building Blocks' 1001 'Wonder Products' 'New York' 'United States' '212 435 1617'
2 4.0031e+05 1002 9 'Painting Set' 1002 'Terrific Toys' 'London' 'United Kingdom' '44 456 9345'
3 4.01e+05 1009 17 'Slinky' 1009 'Doll's Galore' 'London' 'United Kingdom' '44 222 2397'
Close the database connection.
close(conn)
Use a MySQL® native interface database connection to import product data from an outer join between two MySQL database tables into MATLAB®. Specify the row filter condition to use for joining the tables.
Create a MySQL native interface database connection to a MySQL database using the data source name, username, and password. The database contains the tables productTable and suppliers.
datasource = "MySQLDataSource"; username = "root"; password = "matlab"; conn = mysql(datasource,username,password);
Join the two database tables, productTable and suppliers. The productTable table is the left table of the join, and the suppliers table is the right table of the join. The sqlouterjoin function automatically detects the shared column between the tables. Use Type to retrieve records that have matching values in the selected column of both tables, and unmatched records from the left table only. The table data contains the matched and unmatched rows from the two tables.
lefttable = "productTable"; righttable = "suppliers"; data = sqlouterjoin(conn,lefttable,righttable,"Type","left");
Display the first five rows of matched data. The columns from the right table appear to the right of the columns from the left table.
head(data,5)
productNumber stockNumber supplierNumber unitCost productDescription SupplierNumber SupplierName City Country FaxNumber
_____________ ___________ ______________ ________ __________________ ______________ _________________ __________ ________________ ______________
1 4.0034e+05 1001 14 "Building Blocks" 1001 "Wonder Products" "New York" "United States" "212 435 1617"
1 4.0034e+05 1001 14 "Building Blocks" 1001 "Wonder Products" "New York" "United States" "212 435 1617"
2 4.0031e+05 1002 9 "Painting Set" 1002 "Terrific Toys" "London" "United Kingdom" "44 456 9345"
2 4.0031e+05 1002 9 "Painting Set" 1002 "Terrific Toys" "London" "United Kingdom" "44 456 9345"
3 4.01e+05 1009 17 "Slinky" 1009 "Doll's Galore" "London" "United Kingdom" "44 222 2397"
Join the same tables, but this time use a row filter. The filter condition is that unitCost must be less than 10. Again, display the first five rows of matched data.
rf = rowfilter("unitCost"); rf = rf.unitCost <= 10; data = sqlouterjoin(conn,lefttable,righttable, ... "Type","left", ... "RowFilter",rf); head(data,5)
productNumber stockNumber supplierNumber unitCost productDescription SupplierNumber SupplierName City Country FaxNumber
_____________ ___________ ______________ ________ __________________ ______________ ______________________ ________ ________________ ______________
2 4.0031e+05 1002 9 "Painting Set" 1002 "Terrific Toys" "London" "United Kingdom" "44 456 9345"
2 4.0031e+05 1002 9 "Painting Set" 1002 "Terrific Toys" "London" "United Kingdom" "44 456 9345"
5 4.0046e+05 1005 3 "Tin Soldier" 1005 "Custers Tin Soldiers" "Boston" "United States" "617 939 1234"
5 4.0046e+05 1005 3 "Tin Soldier" 1005 "Custers Tin Soldiers" "Boston" "United States" "617 939 1234"
6 4.0088e+05 1004 8 "Sail Boat" 1004 "Incredible Machines" "Dublin" "Ireland" "01 222 3456"
Close the database connection.
close(conn)
Input Arguments
Database connection, specified as any of the following:
MySQL
connectionobject created by using themysqlfunction.PostgreSQL
connectionobject created by using thepostgresqlfunction.DuckDB
connectionobject created by using theduckdbfunction.ODBC
connectionobject created by using thedatabasefunction.JDBC
connectionobject created by using thedatabasefunction.
Left table, specified as a string scalar or character vector. Specify the name of the database table on the left side of the join.
Example: "inventoryTable"
Data Types: string | char
Right table, specified as a string scalar or character vector. Specify the name of the database table on the right side of the join.
Example: "productTable"
Data Types: string | char
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
Example: data =
sqlouterjoin(conn,lefttable,righttable,Type="left",MaxRows=5) performs
a left outer join between the two tables and returns up to five rows of the joined
result.
Left catalog, specified as a string scalar or character vector.
LeftCatalog represents the name of the database
catalog that contains lefttable.
Example: LeftCatalog="toy_store"
Data Types: string | char
Right catalog, specified as a string scalar or character vector.
RightCatalog represents the name of the
database catalog that contains righttable.
Example: RightCatalog="toy_store"
Data Types: string | char
Right schema, specified as a string scalar or character vector.
RightSchema represents the name of the database
catalog that contains righttable.
Example: RightSchema="dbo"
Note
This argument is not valid when conn is an
MySQL connection object.
Data Types: string | char
Keys, specified as a string scalar, string array, character vector, or
cell array of character vectors. Use Keys to
specify common columns used for matching rows.
For one key, provide a string scalar or character vector.
For multiple keys, provide a string array or cell array of character vectors.
You cannot combine this argument with LeftKeys
and RightKeys.
Example: Keys="MANAGER_ID"
Data Types: string | char | cell
Left table keys, specified as a string scalar, string array, character vector, or cell array of character vectors.
Use
LeftKeysto define the columns inlefttablethat match with columns in therighttable.For one key, specify a string scalar or character vector.
For multiple keys, specify a string array or a cell array of character vectors.
Use this argument together with
RightKeys. Both arguments must specify the same
number of keys, and the sqlouterjoin pairs keys
based on their order.
Example: LeftKeys=["productNumber"
"Price"],RightKeys=["productNumber"
"Price"]
Data Types: string | char | cell
Right table keys, specified as a string scalar, string array, character vector, or cell array of character vectors.
Use
RightKeysto define the columns inrighttablethat match with columns inlefttable.For one key, specify a string scalar or character vector.
For multiple keys, specify a string array or a cell array of character vectors.
Use this argument together with
LeftKeys. Both arguments must specify the same
number of keys, and the sqlouterjoin pairs keys
based on their order.
Example: LeftKeys=["productIdentifier"
"Cost"],RightKeys=["productNumber" "Price"]
Data Types: string | char | cell
Maximum number of rows to return, specified as a positive numeric
scalar. By default, the sqlouterjoin function
returns all rows from the executed SQL query. Use this argument to limit
the number of rows imported.
Example: MaxRows=10
Data Types: double
Outer join type, specified as one of the following:
"full"— Returns all records with matching values in the selected columns of both tables, plus unmatched records from both the left and right tables."left"— Returns all records with matching values in both tables, plus unmatched records from the left table only."right"— Returns all records with matching values in both tables, plus unmatched records from the right table only.
Not all databases support every join type. If a join type is
unsupported, use the sqlread function to import data from both tables into
the MATLAB® workspace. Then, use the outerjoin function to
perform the join within MATLAB.
Example: Type="left"
Note
This argument is not valid when conn is an
MySQL connection object.
Variable naming rule, specified as one of the following:
"modify"— Remove non-ASCII characters from variable names when thesqlouterjoinfunction imports data."preserve"— Preserve most variable names when thesqlouterjoinfunction imports data.
Example: VariableNamingRule="modify"
Row filter condition, specified as a
matlab.io.RowFilter object.
Example: rf = rowfilter("productnumber"); rf =
rf.productnumber <= 5;
sqlouterjoin(conn,lefttable,righttable,RowFilter=rf)
Output Arguments
Joined data, returned as a table that contains rows matched by keys in the
left and right database tables and the retained unmatched rows.
data also contains a variable for each column in the
left and right tables.
By default, the variable data types are double for
columns that have numeric data types in the database
table. For any text, date, time, or
timestamp data types in the database table, the
variable data type is a cell array of character vectors by default.
If the column names are shared between the joined database tables and have
the same case, then the outerjoin function adds a
unique suffix to the corresponding variable names in
data.
The variables in data that correspond to columns in the
left table contain NULL values when no matched rows exist
in the right database table. Similarly, the variables that correspond to
columns in the right table contain NULL values when no
matched rows exist in the left database table.
The following table lists how each database data type maps to a MATLAB data type.
| Database Data Type | MySQL | PostgreSQL | DuckDB | SQLite | JDBC/ODBC |
|---|---|---|---|---|---|
| N/A | logical | logical | int64 | logical |
| double | N/A | double | double | double |
| double | ||||
| N/A | N/A | |||
| N/A | N/A | double | ||
| double | double | double | ||
| N/A | N/A | N/A | ||
| |||||
| |||||
| |||||
| double | ||||
| double | double | |||
| N/A | N/A | N/A | ||
| |||||
| double | double | N/A | double | |
| N/A | N/A | |||
| |||||
| |||||
| double | double | |||
| N/A | N/A | |||
| N/A | double | |||
| double | N/A | |||
| datetime | datetime | datetime | cell array or character vectors | |
| N/A | N/A | |||
| |||||
| |||||
| datetime | N/A | |||
| |||||
| datetime | datetime (12AM on specified date) | string | ||
| N/A | N/A | N/A | ||
| double | double | double | N/A | double |
| string | string | string | cell array or character vectors | |
| logical | N/A | N/A | N/A | logical |
| N/A | string | cell array or character vectors | ||
| string | ||||
| N/A | ||||
| |||||
| |||||
| |||||
| duration | duration | duration | ||
| N/A | N/A | |||
| N/A | duration | |||
| calendarDuration | calendarDuration | |||
| N/A | double | double | ||
| |||||
| cell array of uint8 vectors | cell array of uint8 vectors |
| cell array or character vectors | |
| categorical (categories match values of ENUM type) | categorical (categories match values of ENUM type) | N/A | ||
| string | string | N/A | string | |
| N/A | N/A | |||
| calendarDuration | ||||
| char | N/A | |||
| string | ||||
| N/A | Cell array where each cell contains a vector of the corresponding type. For example, DOUBLE[] maps to a cell array of double vectors. | |||
| Each field maps to its corresponding DuckDB type. For example, STRUCT(name VARCHAR, age INTEGER) maps to a MATLAB struct, where name fields are strings and age fields are int32. | ||||
| Cell array of dictionaries. Key and value types match the DuckDB types. For example, MAP(DOUBLE, VARCHAR) maps to a cell array of dictionaries with double keys and string values. | ||||
| Cell array with each cell containing a vector of the corresponding type. For example, DOUBLE[3] maps to a cell array of double vectors. | ||||
| Cell array where each cell can contain a value matching any DuckDB type defined in the UNION. For example, UNION(t TIMESTAMP, d DOUBLE), maps to a cell array where each element is either a datetime or a double. |
Limitations
The name-value argument VariableNamingRule has these limitations:
The
sqlouterjoinfunction returns an error if you specify theVariableNamingRulename-value argument with theSQLImportOptionsobjectopts.When the
VariableNamingRulename-value argument is set to the value"modify":The variable names
Properties,RowNames, andVariableNamesare reserved identifiers for thetabledata type.The length of each variable name must be less than the number returned by
namelengthmax.
Version History
Introduced in R2018aPerform an outer join on two DuckDB database tables by specifying a DuckDB
connection object when using the sqlouterjoin
function.
You can use the RowFilter when joining data from database
tables.
See Also
sqlfind | sqlread | sqlinnerjoin | database | close
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)