%% Join Two Database Tables Using PostgreSQL Native Interface
% Use a PostgreSQL native interface database connection to import product data from an inner 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 sqlinnerjoin function automatically detects the shared column between the tables. data is a table that contains the matched rows from the two tables.

lefttable = "productTable";
righttable = "suppliers";
data = sqlinnerjoin(conn,lefttable,righttable);

% Display the first three rows of matched data. The columns from the right table (suppliers) appear to the right of the columns from the left table (productTable).

head(data,3)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.