Probably because the normal user doesn’t have permissions on the sqlite database. Easy enough to check by going to the directory and checking who is the owner of the files, and who the group owner is.
Since the command works with sudo or as root user, that would confirm that it is due to lack of permissions. And thus, normal situation.
For the record, I can dnf history last as regular user on AlmaLinux 9.7.
All I have is read for that sqlite file:
$ ls -la /var/lib/dnf
total 8596
drwxr-xr-x. 4 root root 4096 Dec 5 14:50 .
drwxr-xr-x. 57 root root 4096 Oct 5 23:44 ..
-rw-r--r--. 1 root root 2457600 Dec 5 14:45 history.sqlite
-rw-r--r--. 1 root root 32768 Dec 5 14:52 history.sqlite-shm
-rw-r--r--. 1 root root 6291272 Dec 5 14:50 history.sqlite-wal
drwxr-xr-x. 2 root root 4096 Apr 30 2025 modulefailsafe
drwxr-xr-x. 15 root root 4096 Jul 15 2024 repos
I avoid running dnf as myself since that creates additional cached files. Hence I did not know whether regular user can dnf history. I do know that dnf refuses some operations, unless user is root.
When launched with sudo it is user root that runs the command (unless you have custom sudoers ruleset).