Join Tables Using Command Line

You can join data in database tables and import the results interactively by using the Database Explorer app. Or, you can join two database tables by using the sqlinnerjoin and sqlouterjoin command line functions. The following short examples show how to join tables using the command line.

Enter this code to create an inner join between two database tables (the left table and right table of the join). An inner join retrieves records that have matching values in the shared column of both tables.

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

To create an outer join, enter this code at the command line. An outer join retrieves the matched and unmatched rows between the two tables. This code shows a right outer join that uses different left and right keys in the corresponding database tables.

lefttable = 'employees';
righttable = 'departments';
data = sqlouterjoin(conn,lefttable,righttable, ...
    'LeftKey','MANAGER_ID','RightKey','DEPT_MANAGER_ID', ...

To join more than two database tables at a time, use the Database Explorer app. For details, see Join Tables Using Database Explorer App. Or, you can create and run an SQL script using the executeSQLScript function.

See Also


Related Topics