%% Limit Number of Rows in Imported Data
% Use a PostgreSQL native interface database connection to import a limited number of rows of product data from a database table into MATLAB®. Then, sort and filter the rows in the imported data, and perform a simple data analysis.
%
% Create a PostgreSQL native interface database connection to a PostgreSQL database using the data source name, user name, and password. The database contains the table productTable.

datasource = "PostgreSQLDataSource";
username = "dbdev";
password = "matlab";
conn = postgresql(datasource,username,password);

%
% Import data from the table productTable. Limit the number of rows by setting the 'MaxRows' name-value pair argument to 10. The data table contains the product data.

tablename = "productTable";
data = sqlread(conn,tablename,'MaxRows',10);

% Display the first few rows of product data.

head(data,3)

%
% Display the first few product descriptions.

data.productdescription(1:3)

% Sort the rows in data by the product description column in alphabetical order.

column = "productdescription";
data = sortrows(data,column);

% Display the first few product descriptions after sorting.

data.productdescription(1:3)

%
% Close the database connection.

close(conn)

% Copyright 2020 The MathWorks, Inc.