diff --git a/web/pgadmin/tools/sqleditor/command.py b/web/pgadmin/tools/sqleditor/command.py
index 3e4549cb1..335911e71 100644
--- a/web/pgadmin/tools/sqleditor/command.py
+++ b/web/pgadmin/tools/sqleditor/command.py
@@ -192,6 +192,9 @@ class SQLFilter(object):
 
             self.nsp_name = result['rows'][0]['nspname']
             self.object_name = result['rows'][0]['relname']
+            self.attname = result['rows'][0]['attname']
+            data_sorting_order = [{'name': self.attname, 'order': 'asc'}]
+            self._data_sorting = kwargs.get('data_sorting', data_sorting_order)
         else:
             raise Exception(gettext(
                 'Not connected to server or connection with the server '
diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/default/objectname.sql b/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/default/objectname.sql
index 188e69db2..c61340364 100644
--- a/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/default/objectname.sql
+++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/default/objectname.sql
@@ -1,7 +1,12 @@
 {# ============= Fetch the schema and object name for given object id ============= #}
 {% if obj_id %}
-SELECT n.nspname, r.relname
-FROM pg_class r
+SELECT pg_attribute.attname, n.nspname, r.relname
+FROM pg_attribute, pg_index, pg_class r
     LEFT JOIN pg_namespace n ON (r.relnamespace = n.oid)
-WHERE r.oid = {{obj_id}};
+WHERE r.oid = {{obj_id}} AND
+indrelid = r.oid AND
+r.relnamespace = n.oid AND
+pg_attribute.attrelid = r.oid AND
+pg_attribute.attnum = any(pg_index.indkey) AND
+indisprimary;
 {% endif %}
